top of page

Coroutines-004 Parameterized Coroutines

Project Files for the course - Download

1.1 Introduction

1. In this tutorial we are going to learn about how to pass parameters to coroutines.

2. As Coroutines are methods we can simply pass parameters to coroutines inside the start coroutine method.

3. Open the Scene004 folder to get started.

1.2 Coroutine with parameter

1. Let us take a look at a coroutine with 3 parameters.

image.png

1.3 Coroutine with parameter

1. Open the ParameterizedCoroutine script.

2. Here we have made a MoveCoroutine method that accepts direction as parameter and moves the cube in the direction after certain delay.

image.png

3. Now let us make two methods called MoveLeft() and MoveRight() from where we are going to pass the direction towards which the cube is going to move.

image.png

4. In the above when we are starting the coroutine we are passing the direction as a parameter before starting the coroutine. If the coroutine is already running then it stops the coroutine first.

5. Now let us add another Method to stop the currently running coroutine.

image.png

6. Let us now test this in the scene.

image.png
image.png
bottom of page