top of page

Coroutines-001-Introduction to coroutines.

Project Files for the course - Download

1.1 Prerequisites

1. Basic Functioning knowledge of Unity Engine.

2. Add Unity Naughty Attributes extension to Unity Engine

1.2 Introduction

Coroutines are unity's way of adding pause to code execution for certain seconds or for time until a certain condition is fulfilled . Using the update method makes this process far more complicated than expected so Unity introduced this concept.

1. For Example if we want to pause the execution of code for 5 seconds then we can use coroutines.

2. If we want to add a pause of 5 seconds then execute some code and then another pause of 3 seconds

3. If we want to execute code with repeated pauses we can use a coroutine.

4. If we want a code to be executed everytime like update loop until some condition makes it break the code we can use coroutine.

So the usecases are endless

1.3 Writing our first Coroutine

1. All coroutines have a return type of IEnumerator and must contain a yield return statement within them.

2. A coroutine could contain more than one yield return statement in it for making different kinds of pauses

Some of the examples are given below

image.png

1.4 Starting and Referencing Coroutines

1. To Start Coroutines we can use the StartCoroutineMethod() within which we can pass the coroutine Method. 

2. There are three ways to starting a coroutine in Unity but we will prefer the method in which we can directly Pass the method as it provides with more flexibility to pass parameters.

3. To store the Started coroutine use the Coroutine variable to store its reference.

Let us take a look at an example

1. Open the scene001 folder and look at CoroutineTester

​

image.png

2. Here we have a coroutine method that takes an input parameter of type int, pauses the code for 0.5 seconds and then prints the value of parameter.

3. Let us now make a method to start the coroutine. To hold the reference to the running coroutine let us create a coroutine variable.

image.png

4. The Button[] attribute allows the three methods to be called from the inspector. Clicking on this button allows us to call the coroutine in Editor.

image.png
image.png

5. Whenever the button is clicked a coroutine gets started and is stored in the firstCorot variable.Let us take a look at the output in the editor.

image.png

6. Code Execution

a. The coroutine is called when the button is clicked and gets stored in the firstCorot variable.

b. It waits for 0.5 second

c. Then the value of a gets printed.

1.5 Types of yield return statements

1. In Coroutines there can be different types of yield return statements that can be used but we will focus only on two of them as other statements can easily be derived from these two 

​

image.png

2. First statement is yield return null-Pauses code for single frame

3. Second statement is yield return new WaitForSeconds(time)-Pauses the code for time amount of seconds.

1.6 Example

1. Let us make an example where when we click the button in the editor cube moves after delay of some seconds by 1.5f in upward direction.

2. Open the scene001 and let us make a script called as Coroutines001.

image.png

3. Here  we have made a coroutine that delays the code for waitTime. After the delay the cube is moved up by an amount of 1.5f .

4. To call this we have made a Method2() method with button attribute so that it can be called from the editor.

5. To hold the reference to this running coroutine we have made a coroutine variable.

image.png
image.png

6. For the scene we have made a cube to which we have attached the Coroutines001.cs script.

7. Let us now run the scene and test it after pressing the button.

​

image.png
image.png

Before pressing the button

After pressing the button cube moves up after a delay of 0.3f seconds.

bottom of page