Day 13 — Getting started with Coroutines in Unity

Connor Fullarton
3 min readMar 27, 2021

--

Hey and welcome.

Today I’m going to be covering a truly fascinating topic and that is Coroutines. Firstly though what exactly is a Coroutine? They’re pretty similar to functions and are used to pause execution of your code right on the line where it’s called and then resume execution after a certain amount of time that you specify.

Where’s the practicality in that? Well let’s say you have an explosion event that only triggers after you’ve walked through a certain doorway and then you’re wanting a separate follow up explosion to happen elsewhere in the room 5 seconds after the initial explosion. One way you can go about getting that follow up explosion to trigger is to have a coroutine setup that starts upon that initial explosion and then waits for 5 seconds before sending out the follow up.

How we’re going to be making use of Coroutines today though is to set it up so that a new instance of our Enemy prefab will be spawning in once every 4 seconds at a random spot along the x axis.

Let’s get straight to it, create an empty object in the hierarchy called Spawner and then create a new C# script called SpawnBehaviour to attach to our empty object and then put in the following code:

[SerializeField]
private GameObject _enemy;
IEnumerator SpawnEnemy()
{
while(true)
{
Vector3 position = new Vector3(Random.Range(-10.7f, 10.7f), 6.2f, 0);
Instantiate(_enemy, position, Quaternion.identity);
yield return new WaitForSeconds(4.0f);
}
}

Remember that for our _enemy variable we need to give it a game object through the inspector so we have [SerializeField] so that we can do that.

You may have noticed that we’re using IEnumerator instead of void here when creating our method. IEnumerator is a Coroutine and is what allows us to use the yield keyword which is what pauses the execution of our code. You’ll see that we’re using this after instantiating the Enemy prefab and we’re using a built in method called WaitForSeconds() which simply tells the code how long to pause things for, in our case here we’re pausing for 4 seconds.

You should be good and familiar with the Instantiate and what’s going into our position variable there with a type of Vector3 but we got something new happening called a while loop.

Loops will be getting their own article in the future but for the time being it’s important to know that a while loop will constantly run the code within so long as its condition is true. So if we were to put something like 3 > 2 that’s a condition that’s always true so the loop will always run and in doing so that creates an infinite loop.

They are quite dangerous though, without that yield in there pausing the loop every 4 seconds it would just keep going until it crashes your computer. It’s handy for us though since we’re pausing the execution in order to make sure that we have enemies constantly spawning for as long as our game is running.

Now if you’ve tried running this code you may have noticed that nothing actually happens, we actually have to start the Coroutine manually in the Update() method and it’s done a little differently to how you would normally initialise a function so add the following into update to get things going.

StartCoroutine(SpawnEnemy());

With that you’ll have something looking like below.

This does clutter up our game a little bit so in the next article I’ll go over how to declutter things.

--

--

Connor Fullarton
Connor Fullarton

Written by Connor Fullarton

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.

No responses yet