As the title says in lecuter 101 of the Complete C# Unity Game Developer 2D course.
I am having an issue whereby for an odd reason, my first wave is repeating itself. I have debugged the process multiple times, although I cannot seem to catch it in the debugger.
Half way through the 4th wave - the first wave happens again without being processed in my debugger (ie. it doesn’t ever hit that part) So I am assuming it has something to do with the yielding of my coroutines.
Has anyone else had this issue?
Below is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
// config params
[SerializeField] List<WaveConfig> waveConfigs;
[SerializeField] int startingWave = 0;
// state Variables
// Start is called before the first frame update
void Start()
{
StartCoroutine(SpawnAllWaves());
}
private IEnumerator SpawnAllEnemiesinWave(WaveConfig currentWave)
{
for (int i = 0; i < currentWave.GetNumberOfEnemies(); i++)
{
var newEnemy = Instantiate(
currentWave.GetEnemyPrefab(),
currentWave.GetWaypoints()[0].transform.position,
Quaternion.identity);
newEnemy.GetComponent<EnemyPathing>().setWaveConfig(currentWave);
yield return new WaitForSeconds(currentWave.GetTimeBetweenSpawns());
}
}
private IEnumerator SpawnAllWaves()
{
for (int i = startingWave; i < waveConfigs.Count; i++)
{
var currentWave = waveConfigs[i];
yield return StartCoroutine(SpawnAllEnemiesinWave(currentWave));
}
}
}