Creating an Enemy Spawn Manager in Unity

Games Woods
5 min readMay 11, 2021
Not this Spawn…we’d need bigger lasers

Now that we learned about Coroutines in Unity it’s time to put them to use in creating our spawn manager.

Objective: Using a coroutine we will create a spawn manager container and script that will instantiate a new enemy in a random position along the X-Axis and a set position along the Y-Axis every 5 seconds.

Implementation: In our hierarchy window we right-click to create an empty object. Name it Spawn_Manager.

In our Scripts folder we right click and create a new C# script. Name it Spawn Manager. Drag the script onto the Spawn_Manager object in the hierarchy. Since we will be instantiating our Enemy prefab we can start the game without the Enemy that is currently living rent free in our hierarchy so let’s delete that bugger.

Now let’s go add some code to our SpawnManager.cs script. First we’ll want to add a serialized field for our _commonEnemy game object. We pop back into the inspector and drag the enemy prefab into the SpawnManager.

Next we want to create our coroutine. So we call an IEnumerator and call it SpawnRoutine().

We need to create an infinite loop. Within it we need to define where the spawn’s will occur. I created a Vector3 called _spawnHere. and for the X, Y and Z axis I create a Random.Range between -8f and 8f, a constant along Y of 7 and 0 for Z.

Then instantiate using the above _spawnHere. Since coroutine’s require that you return control we then yield for 5 seconds.

In order to start our coroutine we need to initialize it. This is accomplished in the monobehaviour start() method by using the StartCoroutine(SpawnRoutine()); call.

Press play and let’s see our work in action. After you’ve been killed you’ll notice that…they…just…keep…coming. Talk about overkill! We will fix that soon.

Optimize Level 1: We I play tested I notice that the enemy’s where being spawn in our hierarchy at the same level as our other game objects. Let’s group them (I think the proper term is child them). First let’s create an empty object and call it Enemy Container. (right click on the Spawn_Manager and Create Empty).

We then want to go back to our SpawnManager script and add a [SerializeField] private GameObject _enemyContainer

Pop back into the inspector and drag the Enemy Container into the new field in the inspector for the SpawnManager script.

Next we have to make a slight adjustment to our coroutine by instantiating into our enemy prefab into a new variable so that we have access to it. We add GameObject newEnemy = in front of our instantiate line. We then access the current parent of our instantiated object and assign it a new one our _enemyContainer.

newEnemy.transform.parent = _enemyContainer.transform;

Now all our enemy spawn within the proper hierarchy we would expect.

Optimize Level 2: Let’s add some logic so that when the player‘s lives reach 0 the enemy’s stop spawning. In order to accomplish this we will need to use some script communication between the Player and Spawn_Manager scripts.

Essentially we want to change our coroutine to stop when the player has reached 0 lives. To do this we need to change its infinite loop to a conditional one.

In our SpawnManager script we will create private bool _stopSpawning and set it to false. This will allow us to change it to true when the player reaches 0 lives.

Next in our SpawnManager script. We want to create a new method called OnPlayerDeath() that when called will change the _stopSpawning bool to true.

Now we change the coroutines’ condition to only run when the _stopSpawning variable is false.

Now back to our Player script. We need to create a global variable _spawnManager that will allow us to assign the SpawnManager component that we are communicating with and wish to change.

We then assign the component in void Start() to _spawnManager and use the GameObject.Find(“Spawn_Manager”).GetComponent<SpawnManager>();

We also add a null check to give us an error if there was a problem with getting that component.

Finally in our Damage() method we ass the call to the OnplayerDeath() method in the SpawnManager script.

And our quick play test confirms that when the player reaches zero lives. no new Enemies are spawned.

--

--