Creating a Spawn Manager Part 1: Coroutines with Unity!

Joe

--

The Enemy is now created and can damage the Player and be destroyed by lasers, but currently only the enemy that is already in the scene exists in the game. To get the enemy to generate itself and move into the scene it needs to be spawned or created. This can be accomplished with a spawn manager that will create and keep track of the enemies.

In the Hierarchy right click and select Create Empty. This will create an empty gameobject in the scene, rename it to Spawn_Manager. This is done because Unity needs an object to attach the script to and so that it can be referenced. An empty gameobject does not draw anything on the screen, it just exists and gives the accessibility to the script that is desired.

Next create a new C# script named SpawnManager and attach it to to Spawn_Manager gameobject.

Create an empty gameobject for the Spawn_Manager and attach the script

The idea in the spawn manager is to spawn a new enemy every few seconds. Trying to do this in the update method is possible but it gets more and more difficult as things progress. So the idea here is to use a Coroutine where you can set the spawn time and forget it. It will take care of everything needed for you. Now to get the Coroutine to work like this it needs to be setup properly.

In Unity this is done with the IEnumerator method setup like so for the Spawn Manager:

IEnumerator Coroutine called SpawnRoutine

I created a variable to generate a specific number of enemies

Variable to set number of enemies to spawn

Inside the IEnumerator method lets create a while loop to run as long as the _numberOfEnemies is greater than 0

While loop which runs when _numberOfEnemies is greater than 0

Inside the while loop is where the Enemy will be instantiated, and the _numberOfEnemies value will be decremented. As was done in the Enemy script a random position will be generated to spawn the Enemy, after that the Enemy script will handle the random starting location after the Enemy has left the bottom of the screen. The same values used in the Enemy Script can be used here. Remember this is done in a Vector3 variable.

Vector3 variable for random starting location of Enemy

Now for my case because I rotated the cube 45 degrees my Instantiate call is different as I needed to adjust the Quaternion to accommodate my rotation along the z-axis. Using the default Quaternion.identity would cause the cube to move diagonally like this:

Using Quaternion.identity when the cube was initially rotated 45 degrees

To fix this and have the Enemy cubes fly straight I adjusted the Quaternion by using the AngleAxis and passing in the rotation of 45 degrees along the Vector3.forward which is the x-axis. So I used this for my Instantiate call:

My Instantiate call to keep my rotated cubes flying straight

This has the Enemies flying the way they were meant to be

Enemies flying straight after the modified Instantiate call

Next within the IEnumerator method I decrement the _numberOfEnemies so that only the total number of enemies wanted are spawned.

Finally in the IEnumerator and what brings this Coroutine together is the yield return in this call we can set the Coroutine to wait a number of seconds before running again. This is done as follows:

Wait 3 seconds before continuing Coroutine

This calls the WaitForSeconds method and waits 3 seconds before going through the while loop again, spawning the next Enemy.

The only thing left to do now is to start the Coroutine.

This is done in the Start method near the top of the script. To accomplish this the StartCoroutine method is called and the name of the IEnumerator method is passed into it, like so

How to call the Coroutine

That is the basics of a spawn manager and how to implement Coroutines in Unity.

In Creating a Spawn Manager Part 1 we will look at organizing the hierarchy with all the gameobject that are spawned, ensuring we keep a clean and uncluttered workspace.

--

--