Wait for previous wave before spawning new wave

I have just finished the laser defender section of the 2d course, and I am really happy with my game.

One thing that I would like to do, which I have no idea where I would even start, is to make the waves wait until the previous wave is destroyed before spawning.

Im guessing this would be added to either the WaveConfig or the EnemySpawner section, but I am honestly not sure where to start. These are the two scripts so far:

Waveconfig:

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

[CreateAssetMenu(menuName = "Enemy Wave Config")]
public class WaveConfig : ScriptableObject
{
    [SerializeField] GameObject enemyPrefab;
    [SerializeField] GameObject pathPrefab;
    [SerializeField] float timeBetweenSpawns = 1f;
    [SerializeField] float spawnRandomFactor = 0.3f;
    [SerializeField] int numberOfEnemies = 5;
    [SerializeField] float moveSpeed = 2f;

    public GameObject GetEnemyPrefab()   {return enemyPrefab;}

    public List<Transform> GetWaypoints()
    {
        var waveWaypoints = new List<Transform>();
        foreach (Transform child in pathPrefab.transform)
        {
            waveWaypoints.Add(child);
        }
        return waveWaypoints;
    }

    public float GetTimeBetweenSpawns()  {return timeBetweenSpawns;}

    public float GetSpawnRandomFactor()  {return spawnRandomFactor;}

    public int GetNumberOfEnemies()      {return numberOfEnemies;}

    public float GetMoveSpeed()          {return moveSpeed;}

}

and my EnemySpawner

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());
        }
    }

}

Any help would be appreciated! :slight_smile:

So Im thinking it needs to be in the EmemySpawner script, in the IENumerator Spawn All waves.

Im guessing we want an If statment that says, If Enemy count = 0, then spawn next wave, but in not sure how to put that into C# code!

Well, actually, you already wrote how it should be implemented. Do you remember what Rick did in the Block Breaker game to count the blocks in the scene? Do the something similar. You know how many enemies are expected to be spawned. You could say: This is the number of enemies in my scene. Whenever an enemy gets destroyed, you call a method that decreases the value. If the value is 0, you spawn the next wave.

You do not have to start a coroutine in Start() if you feel that a solution would be simpler without the Start coroutine. Write your own solution because Rick’s solves Rick’s problems, not necessarily yours.

1 Like

I did not think of that! Let me go back and watch the video, and see if I can work out how to apply it here.

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

Privacy & Terms