Not spawning all the enemies in the wave

My code follows what Rick did, but only the first few enemies get spawned and then no more are instantiated. The ones that are properly follow the path and are destroyed at the end. I added Debug.Log() calls to see what was going on, since I couldn’t figure out how to get the variables to display in the function when debugging. My number of enemies is set to 10, but it only spawns 3, and no error message is displayed on the Console.

Note that the new List() assignment for waveConfigs is just to shut up the warning message for it you would see on the Console.

Here is my code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemySpawner : MonoBehaviour
{
[SerializeField]
List waveConfigs = new List();
int startingWave = 0;

// Start is called before the first frame update
void Start()
{
    var currentWave = waveConfigs[startingWave];

    StartCoroutine(SpawnAllEnemiesInWave(currentWave));
}

/***
*		This coroutine spawns all enemies in a wave.	
***/
private IEnumerator SpawnAllEnemiesInWave(WaveConfig waveConfig)
{
    Debug.Log("Number of enemies to spawn: " + waveConfig.GetNumberOfEnemies());

    for (int enemyCount = 0; enemyCount < waveConfig.GetNumberOfEnemies(); enemyCount++)
    {
        Debug.Log("Spawning enemy #" + enemyCount + " in wave " + waveConfig.name);
        Instantiate(
            waveConfig.GetEnemyPrefab(),
            waveConfig.GetWaypoints()[enemyCount].transform.position,
            Quaternion.identity);
        yield return new WaitForSeconds(waveConfig.GetTimeBetweenSpawns());
    }   // for

    Debug.Log("Number of enemies to spawn: " + waveConfig.GetNumberOfEnemies());
}   // IEnumerator SpawnAllEnemiesInWave()

} // class EnemySpawner

Here is the debug output from the Console:
Number of enemies to spawn: 10
UnityEngine.Debug:Log(Object)
d__3:MoveNext() (at Assets/Scripts/EnemySpawner.cs:26)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
EnemySpawner:Start() (at Assets/Scripts/EnemySpawner.cs:18)
Spawning enemy #0 in wave Wave 1
UnityEngine.Debug:Log(Object)
d__3:MoveNext() (at Assets/Scripts/EnemySpawner.cs:30)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
EnemySpawner:Start() (at Assets/Scripts/EnemySpawner.cs:18)
Spawning enemy #1 in wave Wave 1
UnityEngine.Debug:Log(Object)
d__3:MoveNext() (at Assets/Scripts/EnemySpawner.cs:30)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
Spawning enemy #2 in wave Wave 1
UnityEngine.Debug:Log(Object)
d__3:MoveNext() (at Assets/Scripts/EnemySpawner.cs:30)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
Spawning enemy #3 in wave Wave 1
UnityEngine.Debug:Log(Object)
d__3:MoveNext() (at Assets/Scripts/EnemySpawner.cs:30)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

As you can see, it seems to fail on the fourth enemy. It is in the loop to spawn it, but if the loop had ended properly we would have seen the output afterwards that outputs the number of enemies for the wave again and that is missing.

Never mind. My problem was I changed the [0] in the lecture to be [enemyCount] for the return value from GetWaypoints(). Since I only have 3 waypoints in my path at the moment, trying to address a 4th array element failed. Not sure why you don’t get a runtime error displayed, but that was the problem.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms