top of page

Coroutines-002-Adding multiple delays

Project Files for the course - Download

1.1 Introduction

1. In this tutorial we will learn how to add multiple delays to coroutines in Unity

1.2 Theory

1. To add multiple delays to coroutines we can use multiple yield return statements that allow us to pause the execution of the code multiple times.

image.png

2. For the above code it executes all the three debug.log statement with a break of 3,2 and 1 seconds respectively between each of them.

1.3 Test Scene

1. For the tutorial open the Scene002 folder.

2. Here we are going to be moving cube in different directions with the corresponding wait time between each movement.

3. For this let us open the Coroutines002 script.

4. Let us first make a Move method and then make methods for movement in different directions.

image.png

5. Let us now make a coroutine method and coroutine variable that is going to hold the reference to the coroutine.

6. In this coroutine method we are going to be adding a delay of waitTime between each cube movement. 

7. To move the cube let us make a Method from which we are going to start the coroutine.

8. Add button attribute at the top so that the method can be called from the UnityEditor.

image.png

9. Let us now go to the scene folder and attach the script to a cube and let us now test the movement

image.png
image.png

10. Let us now test the script by running the scene and clicking on the StartMovement() button.

11. As you can see from above that when the button is clicked there is a delay of 0.3 seconds between movement of cube.

image.png
bottom of page