Coroutines with Unity
What are Coroutines? Coroutines allow us to create sequenced events that allow us to yield events. An example of this would be, say we wanted to run an animation, wait 3 seconds, run a banner, and then wait 5 seconds. Coroutines are versatile and will allows us to make unique gameplay. For today’s tutorial we are going to use a coroutine to create an enemy spawner.
To begin the process, we need to start with our pseudocode.
//spawn a game objects every 5 seconds
The Unity Manual has a great definition, and a not-so-great example. The definition states;
When you call a function, it runs to completion before returning. This effectively means that any action taking place in a function must happen within a single frame update; a function call can’t be used to contain a procedural animation or a sequence of events over time.
If you look at the second example you will see that it uses an IEnumerator. An IEnumerator is a coroutine. A coroutine is like a function that can pause execution and return control to Unity but then to continue where it left off on the following frame. We use an IEnumerator method type, because it gives us the ability to use the yield function. Yield is what we use to tell Unity to wait x amount of seconds.
Now that we have a bass understanding of coroutines we can add to our pseudocode.
//Create a coroutine of type IEnumerator- — yield events
We also need to implement a while loop. A while loop is a game loop. A while loop is like a traditional loop, however, this loop will un forever as long as the statement is true. While loops can be dangerous, it can create an infinite loop which will take up the entire memory of Unity and potentially crash your computer. We need to create a break in the loop with yield events.
An example of how to get out of an infinite loop is to create a variable. If we wrote int a = 5, while (a > 0) a- -; So every time the code is ran it would subtract one from int a 5 times until the statement was no longer true.
Now, we can create the IEnumerator method, and lets name it SpawnRoutine().
inside that coroutine we are going to create an infinite loop because we are allowed the yield function. We are also going to instantiate enemy prefab. Remember, to instantiate we need a reference to that object. After creating the object, we want to yield for 5 seconds.
The first step is to right click in the hierarchy and create an empty object and name it Spawn_Manager and create a script and attach it.
Inside the spawn_manager script we need a reference for the object we want to instantiate so we must make a GameObject variable to store the object we want to spawn.
With the Spawn_Manager selected, you can click and drag the enemy prefab from the project folder and drop it into the enemy prefab in the inspector view. Now our object is stored in the GameObject and we can instantiate.
Because this is a video game, we will want to add a random value to the x axis, and to do this we will need to add a Vector3 variable above the instantiate line.
With the posToSpawn variable defined we can add this to out instantiate line.
To finish off this while loop we must add our yield.
The wait for seconds function allows us to us the exact amount of time we would like to yield before the while loop is active again. It may look as though we have a fully fledge Spawning machine, except if you were to start your game now, nothing would happen. We still need to start the coroutine.
To start the coroutine we need to call a special method called start coroutine. We must do this in void start and there are two methods to do this.
Now, when you start your game, you will have a new enemy spawn every five seconds. All with the use of coroutines. As always, take what you’ve learned here and go an experiment with your code. Next time I’ll show you how to tidy up this spawn manager.