Another Way Of Looping All Waves

Hi guys :slightly_smiling_face: !

Here’s a solution I made for those who looking for an alternative relative to what Rick did. You could simply just apply the same technique but with a twist. Instead of letting the toggle(boolean) handle the looping for you, you could instead let the SpawnAllWaves() coroutine handle the whole thing by letting it check if it’s currently at the last wave and then yield itself as shown below :point_down:

private IEnumerator SpawnAllWaves() {
        
    for(int waveIndex = startIndex; waveIndex < waveConfigs.Count; waveIndex++) {

          var currentWave = waveConfigs[waveIndex];
          yield return SpawnAllEnemiesInWaves(currentWave);

         // Reset waves if last wave was spawned
         if(waveIndex == waveConfigs.Count - 1) {
             yield return SpawnAllWaves();
         }
     }
 }

The above code snippet is a basic example of “Recursion”, which simply means when a method or function keeps repeating itself.

Please do let me know if you found this helpful or there’s something I did wrong in the code above.

3 Likes

Privacy & Terms