Learn to code by making a 2D space shooter game.

Suleiman Abdullah
5 min readApr 9, 2022

Objective: Implement Enemy Wave System.

In order to do this, we need to use a custom class to implement this, custom classes are classes that do not inherit mono behavior.

The first step go to the SpawnManager script to create a new class above the SpawnManager class called Wave.

Add system .serializable to be able to see the variable on the inspector

In the next step create two variables int and float variable name them enemy count and time between spawns.

inside Wave class

Then create an array variable of type Wave called waves inside SpawnManager.

In the next step create this variables inside spawnManager, enemy prefab , currentWave, currentWaveNumber,enemiesRemainingToSpawn, nextSpawnTime.

In the next step create a method called Next wave, then we increment the wave by one and then check if the current wavenumber by one 1 is less than waves length we check this to avoid out-of-bounds error of an array last we set enemies remaining to spawn equal to the enemy count.

Solution

This will delay spawning an enemy for 4 seconds when isAsteroidDestroy is true.

Note: Remember to call this function as StartCoroutine in

In the next step go to the inspector to set the amount of the enemy and the time to spawn between enemies.

Now I want to check if the time is allowed to start the wave, but I need to start a wave when the asteroid is destroyed.

so I need to set this boolean to true when the asteroid is destroyed if you have been followed along with my article I have that method called when the asteroid is destroyed so I need to set this bool to true in there.

In the next step, we need to start the NextWave when an asteroid is destroyed, we do this inside the update because we are not sure if we will destroy the asteroid when the game start.

In the next step we need to start spawning when the enemy with the right spawn time, we do this inside the update by checking if we have enemies remaining to spawn and the time is right and don't forget my game needs to spawn when the asteroid is destroyed your you can spawn whatever your condition.

In the next step, we need to make sure we spawn the next wave when all enemies of the first wave died, so we need to go to the enemy and create a delegate, I won't explain what is a delegate is another article in the future but in short delegate, it's a variable which you can assign to the method.

Afte declare a delegate we need to call as normal function when an enemy died.

In the next step, we need to create a variable called enemies remaining alive inside SpawnManager.

Then we need to assign this when NextWave begins, we assign it to enemisRemainingToSpawn.

Remember I have changed this NextWave to coroutine.

In the next step create a method called OnEnemyDeath inside spawnManager then decrements by one, we do this when an enemy dies it decreases the number of enemisRemainingToSpawn.

Then we need to subscribe our delegate action to the method, we do this when we instantiate the enemy, we access the enemy script then access the delegate then we assign it to the OnEnemyDeath method by doing this every enemy instantiated is tracked by the delegate action when enemy die OnEnemyDeath called.

In the next step, we need to check if enemiesRemainingAlive is equal to zero if true we spawn the next wave.

To give a wave visual am gonna implement UI text and disable it and I call the next wave. if you don't how to set check out my UI article.

set empty text when the game start.
Make sure to

Let's see the final result.

--

--

Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development