Wave Spawner: Part 2

Ryan McCoach
3 min readJul 14, 2021

The first thing we will do to start off Part 2 of the Wave Spawner is create an IEnumerator that will take an argument type Wave. IEnumerators allow us to wait a set amount of seconds, almost like a pause, when it is called. This is important for spawning our enemies because we made a spawn rate variable that will be that “pause”. IEnumerators need to return a value, so we will use yield break, which return nothing, for now and set the state to the enum of SpawnState.Spawning.

What we will do next is put pseudocode to remind us we will be actually spawning the enemies, then we will change the SpawnState to Waiting which is the State where the player will need to destroy all of enemies in the wave before changing again.

Back to the update, we will start the SpawnWave IEnumerator where we are changing it the Wave Countdown hits 0 and the state is NOT the already SpawnState is Spawning.

We will pass in the Wave array we declared and the nextWave index into the SpawnWave IEnumerator because it requires those arguments to be met.

Back in the IEnumerator, we will being to create the spawning of enemies. After the state is set to Spawning we are going to loop through the wave’s count to make sure we spawn all of the enemies in that wave’s array.

Now we are going to create a SpawnEnemy method that will take a GameObject argument named _enemy.

Remember we declared a GameObject for the Wave class.

For now, we will print the enemy’s name when it is “spawned” to make sure the code is working properly at this point.

Heading back into the Spawn Wave IEnumerator, we will put inn the SpawnEnemy method passing in the wave’s enemy type and we will wait the set amount of time that we set the wave’s spawn rate to and this will control the amount of time between each of the enemies to be spawned.

That is it for Part 2 of the Wave Spawner, so check back for Part 3!

--

--