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 27 replies, 2 voices, and was last updated 2 months, 1 week ago by Terence.
-
AuthorPosts
-
July 5, 2024 at 3:57 am #15191::
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 #15194::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 #15197::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 #15201::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 #15209::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 #15219July 9, 2024 at 11:06 am #15224::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 #15226::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 #15234::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 #15297::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 #15298::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.September 16, 2024 at 5:06 pm #15837::hey sorry for the long pause, i don’t unsterstand what you mean, i’m pretty new to code.
this is the code snippet that is likely the problem, is everything in here alright?
the same error keeps getting posted in the console line if the game is not paused
void SpawnEnemies() { // Check if the minimum number of enemies in the wave have been spawned if (waves[currentWaveCount].spawnCount < waves[currentWaveCount].waveQuota && !maxEnemiesReached) { // Spawn each type of enemy until the quota is filled foreach (var enemyGroup in waves[currentWaveCount].enemyGroups) { // Check if the minimum number of this type have been spawned if (enemyGroup.spawnCount < enemyGroup.enemyCount) { // Limit the number of enemies that can be spawned at once if (enemiesAlive >= maxEnemiesAllowed) { maxEnemiesReached = true; return; }
// Get a random position from the spawnPositions list for spawning the enemy Vector3 randomSpawnPoint = spawnPositions[Random.Range(0, spawnPositions.Count)]; Instantiate(enemyGroup.enemyPrefab, randomSpawnPoint, Quaternion.identity); enemyGroup.spawnCount++; waves[currentWaveCount].spawnCount++; enemiesAlive++; } } } if (enemiesAlive < maxEnemiesAllowed) { maxEnemiesReached = false; }
}
September 18, 2024 at 12:46 pm #15847::The issue is with this instruction:
waves[currentWaveCount]
If you try and access a
currentWaveCount
that is larger than the number of waves you have assigned, you will get the error that you are seeing.How many waves did you assign?
September 18, 2024 at 3:39 pm #15848 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: