First wave spawns endlessly

I’ve just reached the end of the “Spawn Multiple Enemy Waves” less on and I’m running into an issue where only the first wave spawns, and it continually spawns on an endless loop. here’s my code for EnemySpawner:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemySpawner : MonoBehaviour
{
    [SerializeField] List<WaveConfig> waveConfigs;
    [SerializeField] int startingWave = 0;

    void Start()
    {
        
        StartCoroutine(SpawnAllWaves());
    }
    
    private IEnumerator SpawnAllWaves()
    {
        for (int waveIndex = startingWave; waveIndex < waveConfigs.Count; startingWave++)
        {
            var currentWave = waveConfigs[waveIndex];
            yield return StartCoroutine(SpawnAllEnemiesInWave(currentWave));
        }
    }

    private IEnumerator SpawnAllEnemiesInWave(WaveConfig waveConfig)
    {
        for (int enemyCount = 0; enemyCount < waveConfig.GetNumberOfEnemies(); enemyCount++)
        {
            var newEnemy = Instantiate(
                waveConfig.GetEnemyPrefab(), waveConfig.GetWaypoints()[0].transform.position,
                Quaternion.identity);

            newEnemy.GetComponent<EnemyPathing>().SetWaveConfig(waveConfig);
            yield return new WaitForSeconds(waveConfig.GetTimeBetweenSpawns());   
        }
        
    }
}

I found the solution! i incremented startingWave in the for loop instead of the waveIndex!

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

Privacy & Terms