Wave spawner in Unity

Daniel Kirwan
4 min readMay 10, 2021

Objective: Create an adjustable enemy wave spawner in Unity

What do you need to create a wave spawner for a 2D space shooter?

  1. Abstract class that is serializable
  2. Array of enemyTypes
  3. A time to spawn the next enemy
  4. A wave name

This small class is inside of a new script called WaveSpawner and it sits above the WaveSpawner class.

Create an empty game object in the hierarchy and attach the newly created WaveSpawner script. I am using spawnpoints to randomly spawn enemies into the scene. These are child objects in the wavespawner game object.

To be able to use the abstract class variable you will need to make a reference to it in the WaveSpawner script.

I have also added in the array for my spawnpoints. Now save the script and go back into Unity and you will see options for the variables in the abstract class.

I have created 4 waves and fill in the empty slots with the correct prefabs, the wave names and the number of enemies that need to spawned.

It is now time to set up some more variables that will be used later on. I will need an animator for animating some text, so I will also need a text variable. I will need to know the the currentWave that I am on as well as a currentWaveNumber.

Next is 3 bools that I am using for the conditions to spawn and animate, and a float for the nextTimeToSpawn.

That is all of the variable set up for the class so now let’s dive into how it is working together.

In the update method we are constantly checking whether there are any enemies available to kill. This means that the number of enemies is always updating with the current enemy count in the game scene.

Next there is a check to make sure there are more waves available. It is using the currentWaveNumber +1 and checking it against the number of waves I have in the waves array. Then it is checking whether the animation for the wave completion is true. If this is true, I display the wave name of the next wave in the array and then turn on the animation and set the bool back to false. If there are no more waves to play, then I run my GameOverText method in my UImanager.

Once the animation is completed, an event will automatically run the following method:

The method SpawnNextWave is only run at the end of the animation for the wave completed text that you saw at the beginning of the article.

Now I need to spawn an enemy and making use of the spawnInterval created at the beginning.

First, I am making sure that the canSpawn bool is true and that the nextSpawnTime is less than the amount of time that has passed in game. When this is first run it will always be true.

Then I am selecting a random enemy from the typeOfEnemies array and then choosing a random spawn point from the spawnPoints array.

Next I instantiate the enemy at the random position and make sure to insert it into the spawnContainer that will contain all of the enemies that are spawned.

I then reduce the number of enemies by 1, the next thing to do is to reset the nextSpawnTime float to match the spawnInterval time. I do this by setting it equal to the current time passed in game with Time.time + the spawnInterval float. If 10 seconds have passed and the spawnInterval is 2 then the nextSpawnTime will be 12.

And finally, if the number of enemies is = to 0 then the wave is completed and the wave completed text animation can play.

There is one other method that starts the whole process for me and this is because I start my enemies spawning once a certain action is taken, in this case, when the asteroid is blown up.

That is it for this article about creating an enemy wave spawner in Unity.

--

--