Forum begins after the advertisement:
[Part 6] Trying to spawn the player in a randomly generated map
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 6] Trying to spawn the player in a randomly generated map
- This topic has 11 replies, 2 voices, and was last updated 1 month, 3 weeks ago by
Terence.
-
AuthorPosts
-
July 5, 2024 at 3:57 am #15191
C Vagdalt
Participant::hello, i am trying to spawn my player from the character selector into a randomly generated map, it is currently set up so that the player goes to a loading scene so the map has time to generate, when the map has finished generating you go to the map. the problem we are having is that the player doesnt spawn on the map. i uploaded a video showing the process.
help for this would be really appreciated, thanks in advance
below i will paste scripts that we have for the load scene:
using UnityEngine; using UnityEngine.SceneManagement; public class LevelMove_Ref2 : MonoBehaviour { public string sceneName = "HellMap"; // Specify the scene name to load public WaveSpawner waveSpawner; private void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("Player")) { // Stop current wave spawning if waveSpawner is assigned if (waveSpawner != null) { waveSpawner.StopAllCoroutines(); } // Load the specified scene SceneManager.LoadScene(sceneName); } } private void Start() { waveSpawner = FindObjectOfType<WaveSpawner>(); if (waveSpawner == null) { Debug.LogError("WaveSpawner not found in the scene."); } } }
using UnityEngine; using UnityEngine.SceneManagement; public class SceneLoader : MonoBehaviour { public string[] scenes; public void LoadLoaderScene() { SceneManager.LoadScene("Load Scene"); } public void LoadRandomScene() { if (scenes.Length > 0) { string randomScene = scenes[Random.Range(0, scenes.Length)]; SceneManager.LoadScene(randomScene); } else { Debug.LogError("No scenes assigned to SceneLoader."); } } private void OnSceneLoaded(Scene scene, LoadSceneMode mode) { // Find and initialize the player and wave spawner in the new scene WalkerGenerator walkerGenerator = FindObjectOfType<WalkerGenerator>(); if (walkerGenerator != null) { walkerGenerator.SpawnPlayer(); // Call the SpawnPlayer method } else { Debug.LogError("WalkerGenerator not found in the scene."); } EnemySpawner enemySpawner = FindObjectOfType<EnemySpawner>(); if (enemySpawner != null) { enemySpawner.InitializeSpawner(); } } private void OnEnable() { SceneManager.sceneLoaded += OnSceneLoaded; } private void OnDisable() { SceneManager.sceneLoaded -= OnSceneLoaded; } }
July 5, 2024 at 11:24 am #15194Terence
Keymaster::Can you add the following line above
walkerGenerator.SpawnPlayer()
and see if it prints when the player is supposed to spawn?print("SpawnPlayer() called");
July 5, 2024 at 3:05 pm #15197C Vagdalt
Participant::these are all the messages in the console of the whole process of moving to the map. spawnplayer does get called
July 5, 2024 at 3:53 pm #15198July 5, 2024 at 4:00 pm #15201C Vagdalt
Participant::this is the spawn player function inside of the walker generator
private void SpawnPlayer() { List<Vector3Int> floorTiles = new List<Vector3Int>(); for (int x = 0; x < mapWidth; x++) { for (int y = 0; y < mapHeight; y++) { if (map[x, y] == TileType.Floor) { floorTiles.Add(new Vector3Int(x, y, 0)); } } } if (floorTiles.Count == 0) { Debug.LogError("No floor tiles available for spawning objects."); return; } Vector3Int playerSpawnPos = floorTiles[Random.Range(0, floorTiles.Count)]; Debug.Log($"Player spawn position: {playerSpawnPos}"); GameObject playerObj = Instantiate(playerPrefab, new Vector3(playerSpawnPos.x + 0.5f, playerSpawnPos.y + 0.5f, 0), Quaternion.identity); Debug.Log($"Player instance created at position: {playerObj.transform.position}"); if (cameraMovement != null) { cameraMovement.target = playerObj.transform; cameraMovement.SnapToTarget(); } else { Debug.LogError("CameraMovement script is not assigned in the WalkerGenerator."); } }
July 6, 2024 at 9:07 pm #15209Terence
Keymaster::It seems like your
SpawnPlayer()
function is completely not called. Did you put yourprint()
statement inside of theif
or outside of it?July 8, 2024 at 2:55 pm #15219C Vagdalt
ParticipantJuly 9, 2024 at 11:06 am #15224Terence
Keymaster::Can you check if your Console is showing error messages? If your
SpawnPlayer()
fails to complete, there is supposed to be a line on the Console saying that it has failed, but I don’t see this line appearing in your Console:Debug.LogError("No floor tiles available for spawning objects.");
July 9, 2024 at 2:57 pm #15226C Vagdalt
Participant::these are all the console errors and warnings i get from my start scene all the way to my map scene
July 10, 2024 at 12:48 am #15234Terence
Keymaster::That’s quite a long list of errors which may potentially be feeding into the error that you are facing. Do you know how to get started fixing them?
July 17, 2024 at 2:51 am #15297C Vagdalt
Participant::some of these look like easy fixes, i tried working on the index out of range error before but couldn’t find a solution for it
July 17, 2024 at 6:51 pm #15298Terence
Keymaster::The
IndexOutOfRangeException
occurs when you try to access a value in an array or list that is bigger than the length of the array / list. It usually happens when you are using array indexes.myArray[i];
If the value of
i
is equal to or larger than the number of items in the array, then theIndexOutOfRange
will occur. E.g. if you array has 4 items, andi
has a value of 7. -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: