top of page

Coroutines-003-Starting and Stopping Coroutines

Project Files for the course - Download

1.1 Introduction

1. In this tutorial we are going to learn how to properly start and stop the coroutines.

2. Open the Scene003 to get started with the tutorial.

1.2 Coroutine with yield return null

1. Let us make a coroutine that moves the cube in sinusoidal way in y direction.

image.png

2. To call this coroutine let us make a method and a variable for storing the reference to the coroutine.

image.png

3. Let us take a look at the scene view and call this method.

image.png
image.png

4. To prevent the method from coroutine being called multiple times let us add a null check that doesn't allow the a new coroutine to run when it is already running.

image.png

5. To stop the coroutine let use the StopCoroutine() method in which we are going to pass the already running coroutine.

6. It only stops the coroutine that is running and so it is necessary to have a null check.

7. After stopping the coroutine assign the coroutine variable to a value of null.

image.png

8. Let us now test the stopping functionality.

9. Now we are able to successfully able to stop the coroutine by pressing the stop button.

bottom of page