Skip to content

Coroutines

Give a component time. Wait a second, pulse a scale, fade a color, or chain a few steps, then keep editing the same object in Play Mode.

ZenCoder runs the routine for you. Press Stop, disable the component, or press Run again, and every routine that component started is cancelled. The scene stays clear of leftover waits from the previous version of your code.

1.2.0 · Shoshin

Introduced in the v1.2.0 release.

Start one from any method

Start, Update, and the other lifecycle methods stay ordinary methods. From any of them, hand ZenCoder a routine:

public void Start()
{
    StartCoroutine(Intro());
}

private IEnumerator Intro()
{
    yield return new WaitForSeconds(1f);
    transform.position += Vector3.up;
}

Press Run. One second later, the object steps up.

IEnumerator is ready to use. No extra using line is required.

You can also start a routine by its method name, when that method has no parameters:

StartCoroutine("Intro");

Let Start itself be the routine

If the wait is the whole beginning of the component, change void to IEnumerator:

public IEnumerator Start()
{
    yield return new WaitForSeconds(1f);
    transform.position += Vector3.up;
}

ZenCoder runs that once when the component starts. The lifecycle menu can insert this for you: open + and choose Initialization → Start Coroutine. That entry is there when the script does not already have a Start method. If Start is already in the script, change void to IEnumerator yourself, or call StartCoroutine from the method you already have.

Update, FixedUpdate, and the other messages stay void. A repeating method would begin the routine again every frame. Start the routine once, from Start or from the frame where the player presses a key.

A routine you can trigger

public void Update(float deltaTime)
{
    if (Input.GetKeyDown(KeyCode.Space))
        StartCoroutine(Pulse());
}

private IEnumerator Pulse()
{
    transform.localScale = Vector3.one * 1.2f;
    yield return new WaitForSeconds(0.15f);
    transform.localScale = Vector3.one;
}

Each press of Space grows the object, holds for a moment, then returns it to its original size.

Stop one, or stop them all

Keep the handle when you want to cancel a single routine later:

private Coroutine pulse;

public void Update(float deltaTime)
{
    if (Input.GetKeyDown(KeyCode.Space))
        pulse = StartCoroutine(Pulse());

    if (Input.GetKeyDown(KeyCode.Escape))
        StopCoroutine(pulse);
}

StopAllCoroutines() cancels every routine this component started, including an IEnumerator Start().

Stop, turning the component off, or pressing Run again ends them for you as well.

Ways to wait

Inside a routine:

  • yield return null waits one frame
  • yield return new WaitForSeconds(1f) waits in game time
  • yield return new WaitForSecondsRealtime(1f) waits on the clock, even if Time Scale is 0
  • yield return new WaitUntil(() => grounded) waits until something becomes true
  • yield return new WaitWhile(() => moving) waits while something stays true
  • yield return new WaitForEndOfFrame() waits until the frame has finished drawing
  • yield return new WaitForFixedUpdate() waits for the next physics step

You can yield return another routine to run it as part of this one:

yield return Pulse();

While you type

  • sta offers StartCoroutine
  • sto offers StopCoroutine and StopAllCoroutines
  • yie offers yield return
  • After yield return, ZenCoder offers the waits above

A routine that throws is covered by Safe Execution: the first failure pauses the script, and later frames stay quiet.


← Lifecycle Methods Hot State →