Adding Enemy Spawn Waves to 2D Galaxy Shooter in Unity-Part 01!

Objective: To implement a 5 wave system that spawns an increasing number of enemies with each wave.

With just a few final features to be implemented before I can call this game finished, let’s take a look at building a spawn system that flows sequentially when one wave ends and the next starts. For this feature we aren’t going to use multiple scenes and instead take the current scene and use a modular coroutine to handle the spawning waves. Let’s get into it…

Let’s start with some simple variables that are going to handle how many _remainingEnemies are in each wave and also the _currentWave the player is on.

The _stopSpawning bool is going to be the trigger switch for when the enemy and powerup spawning routines will start and stop.

We also need a couple of GameObject arrays for the _enemies and the _powerups. These are serialized so we can drag and drop these game objects in the Unity Inspector.

Let’s cache our _enemySpawnTimer WaitForSeconds class as we are going to be using this a bit and don’t want to keep calling ‘new’.

_uiManager is simply going to give us access to this script so we can call a UI coroutine that handles the display text for ‘Wave 1’ and ‘Wave 2’ etc.

_co is going to cache a powerup coroutine that we are going to call and we also need to be able to stop this without calling StopAllCoroutines(), as we have some in the background that we want to keep running.

Start() is going to grab the UIManager script component so we can call the coroutine that runs the ‘Wave 1’ and ‘Wave 2’ as mentioned above. Don’t forget to null check!

Let’s also set our _currentWave to 0 so that game doesn’t automatically start when we press play. Let’s give the player the ability to start the game by shooting the Asteroid first.

Within Update() we are going to be checking constantly for _remainingEnemies so we can update the UI display number.

This is the UpdateEnemiesRemaining() method that we are calling from the “UIManager” script:

This OnTriggerEnter2D() method is on the “Asteroid” script and is looking for trigger detection with the player’s laser. The StartSpawning() method (below) is called when the trigger occurs.

This method increases the _currentWave from 0 to 1 so that when we call the NextWave() method (below) it will start at ‘Wave 1'.

This method is called in 2 separate places, once at the start of the game to commence the first wave from within StartSpawning() method and the other times are at the end of the SpawningEnemiesRoutine() coroutine when the _remainingEnemies is < 1.

The _currentWave variable is used as the comparison expression for the switch statement and determines which wave to spawn when a matching number is found.

Then it will call the 2 coroutines, one which displays the Wave number and the other starts the spawning enemies and powerups (both requiring parameters).

Next Article: The second and final part to the enemy spawn feature.

Aspiring Unity Game Developer with a passion for bringing code to life!