Coroutines. Coroutines? Coroutines!

addam davis
2 min readJul 19, 2021

--

Objective: Understand what a basic coroutine is and how to implement in the program.

What is a coroutine? Coroutines are functions that can suspend its execution or “yield” until the yield instructions is finished. To use a coroutine you must use a special type called IEnumerator.

The IEnumerator is red because it not returning a value. So for the example lets us a Wait For Seconds yield.

This is what a barebones Coroutines looks like. The coroutine is best used when you have something you want to happen sequentially.

With this we have a coroutine that will display “My Name,” wait three seconds then display “Your Name.”

With the current state of the script the code will not run. We need to start the coroutine. You Cannot call a coroutine like you would a typical method. For a coroutine you need to use the key words Start Coroutine.

There are two different ways to start a coroutine. A string, or by the IEnumerator name.

The string method is slightly slower, however, using a string is the only way you can stop a coroutine. So if you need to set a circumstance that stops the coroutine you would need to use the string method.

This is all there is to setting up a coroutine. With this you can easily set up a series of events. Don’t be afraid to experiment with these functions, and I’ll see you in the next tutorial.

--

--

No responses yet