Learning Unity — Introduction To Coroutines

Joshua Nielsen
3 min readJun 10, 2021

--

In the last article, we learned about communicating between different scripts using the GetComponent method. In this article, we’re going to continue levelling up our scripting skills by taking our first look at coroutines.

Coroutines are Unity’s way of allowing us to run some code over a specific period of time. Say for instance you wanted to have an object fade from view over two seconds or you wanted to have something be created every five seconds. This is what coroutines are made for.

Naturally, we will need a game object and attached script. Once you have your script open, we have a few requirements in order to use coroutines:

  1. First, the coroutine has to be a method of type IEnumerator. Being of type IEnumerator is what allows the coroutine to pick up in its next cycle where it had left off before.
  2. Second, being of type IEnumerator, it needs a yield return statement. Explanation of the yield return statement can be found in the Microsoft C# documentation.
  3. Third, your coroutine needs to contain some sort of loop. Without a loop, the code in the coroutine will only run once, and that may defeat the purpose of using a coroutine in the first place.
  4. Finally, your coroutine must be initiated through the use of the StartCoroutine method.

Let’s see a simple example in action. In the script, I have created a variable named _renderer that references the object’s Renderer. Then I have created this coroutine:

Coroutine that reduces the alpha value of the object’s color

What this code does is it lowers the alpha (opacity) value of the game object by 0.1, then waits 0.1 seconds and runs again. In order to make this coroutine actually run, I inserted into the Start method of the object a call to the StartRoutine method. In this case, the line will read:

StartRoutine(“FadeCoroutine”);

Once that is done and the Unity editor compiles the script, we get this as the result:

The fade coroutine in action

The coroutine runs through the loop we have provided until it completes it in ~1 second, and then the coroutine is done. Perfect!

NOTE: The Rendering Mode option of the game object’s material must be set to “Transparent” for the fade effect to work.

But what if we wanted something to continue indefinitely? Like if you wanted enemies to continue spawning until the game was over? Not a problem!

Coroutines in Unity are one of the few times in coding where an “infinite” loop is acceptable. All you need to do is create a loop using while(true) and the coroutine will continue running indefinitely. Even better would be to create a boolean variable to represent an end condition for the coroutine, such as while(gameIsNotOver).

Here I have created a new coroutine that will continue changing the color of the game object indefinitely:

A coroutine for randomly changing the color of the game object

If you do need to stop a coroutine, either because it’s running infinitely or even before it would normally end on its own, there is the StopRoutine method for that. Like the StartRoutine method, the StopRoutine method takes the coroutine’s name as a parameter and can be called at any time.

And that wraps up our introduction to coroutines. In the next article, we’re going to look into a method of keeping our projects organized and tidy. Until then, good luck and happy coding!

--

--

No responses yet