Coroutines(IEnumerator)

Chris Didier
2 min readMar 31, 2021

--

A coroutine gives the ability to wait a certain amount of time before continuing forward with an operation.

To show this I’m going to print out my name after 3 seconds. If you use a string you can stop a coroutine if it is in progress. If you a coroutine more than once it can reset the coroutine before it ever finishes.

Now I created a program that after 3 seconds my name is printed and after another 2 seconds a 2nd name is printed. Using the coroutine functionality.

Next I am creating a program that turns the cube invisible for 5 seconds and then re-appears when the “i” button is pressed. If the cube is already hidden the I key cannot be pressed.

Warning might run into problems if called before finishing

Create a program where the color of the cube changes every 3 seconds to a random color.

There is also a way to improve upon what was written by doing cashing the wait for seconds. See the example below.

Coroutines are a vital point of a game as there are many actions within a game that happen on a timer.

--

--

Chris Didier
Chris Didier

No responses yet