Time Between Waves

Hello everyone.

We’ve implemented a timeBetweenSpawns variable that add time between each enemy of a given wave, but what I want to do is to add time between each wave.
I’ve tried to figure it out, adding a Coroutine after the SpawnAllEnemiesInWave() method, but it’s not working.

Any ideas?

Hello Andres,

Perhaps if you provide a little more information someone can help you. Consider mentioning what does happen after the changes you made and also include your code.


See also;

Yes, sorry, I just assumed that everyone may have the code Rick taught on the course.

So, this is the EnemySpawner.cs that Rick explained on the Laser Defender course

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

public class EnemySpawner : MonoBehaviour {

    [SerializeField] List<WaveConfig> waveConfigs;
    [SerializeField] int startingWave = 0;
    [SerializeField] bool looping = false;
    

	// Use this for initialization
	IEnumerator Start () {
        do
        {
            yield return StartCoroutine(SpawnAllWaves());
        }
        while (looping);
	}

    private IEnumerator SpawnAllWaves()
    {
        for (int waveIndex = startingWave; waveIndex < waveConfigs.Count; waveIndex++)
        {
            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());         
        }        
    }   
}

This code gets some values from another script called WaveConfig.cs.

In here, the for loop makes enemies in a wave appears one after the other with a separation of time defined in the variable timeBetweenSpawns defined in the WaveConfig.cs

I would like to add another timer that separate waves so I simply created a Coroutine and a serializable variable

    [SerializeField] float timeBetweenWaves = 10f;

 IEnumerator TimeBetweenWaves()
    {
        yield return new WaitForSeconds(timeBetweenWaves);
    } 

and I’ve added that code to the EnemySpawner.cs script. Then I’ve inserted the StartCoroutine(TimeBetweenWaves()); after the for loop in SpawnAllEnemiesInWave() but it doesn’t seems to do the trick.

I’m not sure if it’s more clear now. I have the exact game Rick explained on the course, just wanted to add more time between waves.

Hey,

Not every one using the forum will be taking the same courses, or at the same place in the course, so it’s always worth giving as much information as possible as you are likely to get support sooner :slight_smile:

I’m responding via my phone and haven’t looked at the project but try adding your additional coroutine as indicated below;

private IEnumerator SpawnAllWaves()
{
    for (int waveIndex = startingWave; waveIndex < waveConfigs.Count; waveIndex++)
    {
        var currentWave = waveConfigs[waveIndex];
        yield return StartCoroutine(SpawnAllEnemiesInWave(currentWave)); 
        // add your new coroutine to delay wave here           
    }        
}

If this doesn’t work let me know and I will take a more detailed look later when I’m back at the keyboard :slight_smile:

mmm no, it seems it’s ignoring my coroutine :frowning:

Thanks a lot for the help, Rob :smiley:

Hey,

Ok, can you zip up your project files and share them with me, I will be back at the desk in about 10 minutes and will be happy to take a look.

The forum will allow uploads of up to 10MB, if your project files (zipped) are larger than that you would need to use a service such as Google Drive or Dropbox, and then share the URL.

Here it is!

Laser Defender

Hi,

Ok, so I’ve added a couple of Debug.Log statements so we can see what is happening, I’ve also dialled the 100f delay down to 5f, in the Inspector.

IEnumerator TimeBetweenWaves()
{
    Debug.Log("TimeBetweenWaves Starting");

    yield return new WaitForSeconds(timeBetweenWaves);

    Debug.Log("TimeBetweenWaves Finishing");
}

The code is running as it should, TimeBetweenWaves gets called, 5 seconds pass, then the second Debug.Log statement is produced.

What you don’t have in TimeBetweenWaves though is anything that really does anything, so you call your coroutine and it says ok, I’ll just pop off and do this then. Meanwhile, everything carries on.

If you change the line of code where you call the coroutine from this;

StartCoroutine(TimeBetweenWaves());

to this;

yield return StartCoroutine(TimeBetweenWaves());

You will get the wait effect that you want.

That method would look like this;

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

        yield return StartCoroutine(TimeBetweenWaves());
    }
}

Hope this helps :slight_smile:

Fantastic!!! it works like a charm!!!

I was so close … but I didn’t know I can use more than one yield return … I don’t know why I just assumed that I could use only one.

Amazing, it’s working now and I’ve learning something new. Thank you very very much :smiley:

1 Like

You’re very welcome Andres :slight_smile:

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

Privacy & Terms