Currently, enemies in my space shooter will spawn forever as long as the player is still alive. To change things up, I want to add a wave spawning system to the game where each wave will progressively spawn more enemies. Maybe later I’ll add a boss level to the end.

Most of the work will be done in the spawn manager script. I started by making an integer to track the current wave and an array of integers with the number of enemies to spawn for each wave. I modified the StartSpawning method to handle wave spawning. It increments the current wave and updates a wave counter on the UI with the current wave. It also stops all running coroutines, then restarts them. I don’t want two instances of the same routine running at once.

SpawnManager.cs

Wave logic will be handled in the enemy spawn coroutine. First, it gets the number of enemies to be spawned from the array I created earlier. Then it goes through a for-loop to spawn to spawn all the enemies. When all enemies have been spawned, the spawning stops and a 10-second countdown is started. After 10 seconds, the next wave starts by calling the StartSpawning method.

SpawnManager.cs

You’ll notice there’s also a wave timer being called on the UI Manager. There’s a coroutine in that script to show a timer on the screen before the next wave starts.

UIManager.cs