Sense of Coroutines

Dear all,

I try to understand the sense of a coroutine. I want to call a coroutine in update() and in the coroutine I want to define the time, update waits for calling the coroutine.

IEnumerator Coroutine()
{
Debug.Log(System.DateTime.Now);
yield return new WaitForSecondsRealtime(5);
Debug.Log(“I’m later”);
}

void Update()
{
StartCoroutine(Coroutine());
}

The result of this is, that the coroutine will be called as often as every function will be called from update(). Only what comes after the yield line, is waiting.

I can delay a function call without a need of a coroutine.

timer += Time.deltaTime;
if (timer > waitingTime)
{
StartCoroutine();
timer = 0;
}

So if I’m not wrong, what is the sense of a coroutine?

Thank You very much.

Starting coroutines every frame does not work.
Here’s more info about it: Execute coroutine in Update() - Questions & Answers - Unity Discussions

It’s true that pretty much any coroutine can be written as delays in Update. However, you need to maintain extra state (your “timer” and “waitingTime” variables). If you have 10 things you want to delay, you’ll have to keep track of 20 variables.

Coroutines make things easier when there’s something you want to happen once. For example:

  • Play a sound or animation that lasts 1 second. Start a coroutine to also wait 1 second, and perform some action at the end.

  • When an object leaves the screen, wait 10 seconds in a coroutine. At the end, if the object is still off-screen, destroy it.

  • Create an object on-screen 1/5 of a second after a mouse click.

Coroutines are also useful when you want to perform several actions in a row. Maybe you want to create a character, have it dance for 10 seconds, then have it jump for 10 seconds, then have it explode. You could do that with a state machine in Update, or you could use a coroutine with multiple yields.

Coroutines are a shortcut. Use one when you think it will simplify your code. But don’t create too many, because there are performance implications.

Coroutines actually go ahead and create their own objects, which have their own scope and live their own lives, mostly independent of the rest of your script. They die on their own too.

This gives them several advantages

  • Clutter. Coroutines mean you can write code once, which makes it cleaner. There is no need to write multiple if statements. Update looks and feels much simpler.
  • State. Coroutines can hold their own variables. Which means you don’t need to allocate memory at the class level.
  • Mutiples. You can run multiple versions of the same coroutine with no special penalty.
  • Sequence. Coroutines are FSMs. That makes them ideal to run a bunch of sequential steps.
  • Set and forget. Once a coroutine is running, you can safetly ignore it.
  • Auto termination. A coroutine will not check the timer if its not running. With a lot of them running, it can add up.
1 Like

They let you execute asynchronous code in a synchronous manner. Thats all they do. But its also a very powerful feature. We use it alot for micro workflows in our game.

For example I built our ingame Tutorial on it, works very well. Each step in the tutorial is a scriptable object with a IEnumerator Execute() method. Then I have a single Coroutine that iterates over these

Many Thanks,

that’s a lot of stuff I’ve to deal with!

Many Thanks!