Hmm…Wave sequencing, why don’t we try and start with something that should be fairly simple? Let’s make it so that the first wave spawns only 5 enemies, second 10 enemies and the third unlimited.
In order to accomplish this we should probably create a new script and call it; let’s say; WaveManager. In this script we will need some more variables, let’s start with:
Next we will need to add a variable to our SpawnManager script, such as:
Then we need to add:
to our SpawnEnemyRoutine() co routine. Saving the script and jumping back into Unity and pressing play, will give:
Awesome, now we have a variable keeping count of how many enemies are spawned, how can we use it? Well, first of all we need to tell our WaveManager script about our SpawnManager script, by making a reference to it, in the Start() method of the WaveManager script:
Also, when our first level is loaded, we will need to set _wave = 1. This can be done in the Start() method as well.
Next, in order to set the _enemiesSpawned variable of the WaveManager script, we need to add the following to the Update() method:
Then, in order to stop the enemies spawning we need to add the following to the Update() method:
What!!! Where did this StopEnemySpawn() method come from? It is the method that will need to be created to stop the enemies spawning (Dahh!!); I will leave that to you. Now, saving both scripts and jumping back into Unity and pressing play, will give:
Fantastic!!! Only 5 enemies were spawned, now we just need to figure out how to let the game know; that once all enemies are destroyed; to end the level. I will tackle that in Part 2.