Creating Enemy Spawn Manager in Unity
For today’s article, I’ll be going over how I implemented a system that spawns a group of enemies divided into waves using Scriptable Objects. To see the full source code, please visit: https://github.com/hlimbo/Space-Shooter-Tutorial
Scriptable Objects
In Unity, a Scriptable Object is a data container that holds information relating to your game. It is stored as a file asset in Unity and allows us to configure specific data values per file instance. To learn more in depth about the various use cases Scriptable Objects can have for your games, please visit: https://unity.com/how-to/architect-game-code-scriptable-objects for more information.
For the game above, we are using Scriptable Objects to model what spawning a wave of enemies would look like:
enemyPrefabs
hold a list of enemies to spawn for the given wave and pathContainer
is a prefab that holds game-object children that each contain a path an enemy can follow.
Wave Manager
The next thing I created is an empty game-object in Unity’s hierarchy view named WaveManager
The WaveManager
is responsible for spawning enemies, possible paths enemies can take, and setting what wave the player will be currently on. One of the problems I ran into when implementing this system was when I should be starting the next wave. I had solved that issue by setting the rule that the current wave ends when all enemies are marked as destroyed. Enemies can be destroyed by leaving the camera view or when the player shoots them down.
In WaveManager
I created a public function called DecrementEnemyCount
which is called in the Destructible
script’s onDestroy
function to reduce the number of enemies left in the current wave.
Once the activeEnemyCount
counts down to 0, a new wave will be started by the WaveManager
Summary
Putting everything together, the Enemy Wave Manager system with Scriptable Objects will allow me to:
- Create as many waves as I want by dragging and dropping enemy prefabs and enemy pathways
- Specify the number of paths an enemy can follow via Path Container field
- Configure Spawn Delay and Wave Transition Delay via
WaveManager
script
Thanks for reading :)