2D Galaxy Shooter — Making a Clutterless Spawn Manager using Coroutines
Today, I will be going over how to make a spawn manager for our enemies.
The main staple in a space shooter is the ability to shoot at endless enemies, am I right!? This is the last step of the prototype stage, let’s finish this!
Creating the Spawn Manager
Create an “Empty” GameObject, name it Spawn_Manager, then set its location to 0x, 0y, 0z.
Spawn_Manager Behavior using Coroutines
Coroutines are used to have an event wait for a set amount of time. This can be used for cooldowns in a RPG, timed explosives like grenades, or even a flickering light. Coroutines require an “IEnumerator” method with a “yield return” somewhere inside to run. They also require a StartCoroutine() to start.
- Create a C# script, name it SpawnManager, then add it to the Spawn_Manager in the Hierarchy.
- Create a variable — [SerializeField] private GameObject _enemyPrefab;
- Back in unity, select the Spawn_Manager, go to the Inspector, find the script component, then drag and drop the enemy from the prefabs folder in the box that asks for the “Enemy Prefab”.
- Start a new method of type “IEnumerator” and name it “SpawnRoutine()”.
Inside the SpawnRoutine() method, add the above code.
We are using a while (true) statement to make an infinite loop since we want enemies to always spawn.
- Note, infinite loops can be dangerous and may crash Unity. The reason they are used here is because we can stop it by using a “yield return”.
Inside the while loop, it’s saying that we instantiate enemies at (random range of -9.5f to 9.5f x, 7.4f y, and 0z), similar to the enemies respawn variable when they reach the bottom of the screen.
So while true, it spawns an enemy, then runs the “yield return new WaitForSeconds(5.0f)”. This is a required part of a coroutine and allows the infinite loop to stop for 5 seconds. After 5 seconds pass, it will run the while loop again.
Last thing, to start the coroutine, head to void Start and write: StartCoroutine(SpawnRoutine());. Without this, you can’t start a coroutine.
Cleaning up the Clutter
If you run your program and look at your Hierarchy, the enemies are spawning over and over. This can be troublesome when debugging when you have several GameObjects. Let’s start by making the Spawn_Manager a parent.
- Create another “Empty” object, name it Enemy_Container, then drag and drop it on the Spawn_Manager.
- Open the SpawnManager script.
- New variable — [SerializeField] private GameObject _enemyContainer;.
- Back in unity, select the Spawn_Manager, go to the Inspector, find the script component, then drag and drop the Enemy_Container from the Hierarchy in the box that asks for the “Enemy Container”.
5. In our while loop, lets make our Instantiate statement a local variable, type GameObject and named “newEnemy”.
6. By using newEnemy as a local variable, we can make the transform of the new enemies that spawn children of the Enemy_Container, thus controlling the clutter of our hierarchy with the new collapsible Enemy_Container.
- This is also useful because we will be adding powerups in the Spawn_Manager.
Stop Spawning After Player Death
After you die, what would be the point of having the enemies spawn more? I don’t see any reason, so let’s fix it!
- Head to the Player script.
- Make a new variable — private SpawnManager _spawnManager;.
- Head to the Start method.
4. Add this code under our starting position.
- This implication of GetComponent uses GameObject.Find(“”) to find the GameObject you need using a string of the exact GameObject name, as seen above.
- Using the best practice, if the Spawn_Manager isn’t called here, it will pop up an error message saying that “The Spawn Manager is NULL”.
5. Jump to the SpawnManager script.
6. Make a new variable — private bool _stopSpawning = false;
7. In our while loop, instead of it being true, make it:
while (_stopSpawning == false), this means its true till the player dies.
8. Create this new public method called OnPlayerDeath().
9. Now head back to the player script, and go to the Damage() method.
10. Add the _spawnManager.OnPlayerDeath() to our “Death” if statement.
- This is calling the SpawnManager’s OnPlayerDeath() method using GetComponent.
This was how to make a spawn manager!
Next, we will be leaving the prototype stage! This means we use 2D objects, like sprites. It’s gonna get fancy here!
Thank you for your time!