Spawn Manager

Gert Coppens
Nerd For Tech
Published in
5 min readMay 24, 2021

In this article I will explain the steps I’ve taken to implement a basic Spawn Manager to the Space Shooter Project by using a powerful method called Coroutines. By assigning the spawning enemies to a new parent we keep the hierarchy overview clean and prevent it from clustering. We’ll also learn how to create a reference to the Player Script in order to prevent the Spawn Manager from spawning enemies if the Player is dead.

Spawn Manager Script

By creating a Spawn Manager we can spawn our enemies automatically so we wouldn’t have to set them up manually in the Scene.

Start by creating a new script for the Spawn Manager and attach it to a new empty GameObject called “SpawnManager”.

Now let’s store the Enemy Prefab we want to spawn into a private GameObject variable. In the new SpawnManager Script we create a reference to that Enemy Prefab by creating a private GameObject _enemyPrefab;

Then drag the Enemy Prefab from the Project window into the new slot on the Spawn Manager Script in the inspector.

To spawn the enemies I will introduce you to a new concept, Coroutines.

This is a special kind of function that can wait to execute its functionality accordingly to given instructions and resume from where it has left off on the next frame.

Coroutines have the ability to include a statement called Yield, yielding of any type, including null, results in the execution coming back on a later frame, unless the coroutine has stopped or has completed.

Before we set up our Coroutine we need a boolean type _stopSpawning which is set to false by default.

To set up our Coroutine we create a method “methodName” of Type IEnumerator in which we will model a while() loop that will run its code if _stopSpawning is set to false.

In the while() loop we declare which object we’d like to instantiate and define a new spawn position as well as its orientation. For now, I’ve assigned a random position to the new spawn position for the enemies that we instantiate.

In the end we make use of the Yield return new WaitForSeconds(5.0f) statement to indicate the pause in between rerunning the code. In our case 5 seconds.

Since the code in the while() method runs in a loop we also need a separate function to start the actual Coroutine method.

In the Start() function we start our SpawnRoutine() at the beginning of the game.

Important Note: While Coroutines are great for code clarity and declaring behaviour over several frames, they impose a higher memory overhead due to the fact the CPU code of Coroutines always appears in two places in a trace. It’s a good practice to condense a series of operations down to the fewest individual Coroutines as possible.

We will examine the best usage of Coroutines more thoroughly in a different tutorial series devoted to optimization in Unity.

Let’s keep it clean
To prevent the hierarchy from clustering we should create a new parent for the enemies to be attached to when they spawn at runtime.

Create a new private GameObject _enemyContainer and attach it as a child to the SpawnManager in the Hierarchy window.

Then we set its new parent to be the _enemyContainer in the while() loop that instantiates the enemy.

We should also cache the New Enemy in memory by storing it in a local variable and as last we can declare another variable that is adjustable on the Component in the inspector so we can easily adjust the spawn rate.

Player Script

In the Player Script we must make some modifications to stop the SpawnManager from spawning enemies when the Player dies.
However the SpawnManager has its own Script, so we need to get a reference to it by the use of script communication.

First we create a new private member variable SpawnManager _spawnManager; to cache the SpawnManager once we grabbed it.

In the Start() method we fetch the “SpawnManager” but first we perform a null-check to make sure there is one present, else we print an error.

If you’re not familiar with the GameObject.Find() & GetComponent<SpawnManager>(); methods I’d like to invite you here, where I’ll introduce you to script communication in Unity.

To Stop the Coroutine method in our case we will first create a OnPlayerDeath() method in the Spawn Manager Script that simply sets the condition of _stopSpawning to true. Note that there are multiple ways to Stop a Coroutine.

For the SpawnManager to stop spawning Enemies when the Player dies, we then add _spawnManager.OnPlayerDeath() function to our Damage() method in the Player Script.

_stopSpawning is set to false in OnPlayerDeath() when the player dies

Previous | Next

--

--

Gert Coppens
Nerd For Tech

Software Engineer — Unity Game and Application Developer