Eonevolve Learning
Unity and DevelopmentUnity and codeAdvanced~6 min

Coroutines vs State Machines: Sequencing Without Blocking Update

Snapshot

~80 sec

A coroutine sequences a linear wait-then-do script without blocking Update. An explicit state machine names each state so other systems can ask "what state are we in" at any time. Short, one-shot sequences favor a coroutine; anything another system needs to query favors a named state.

You will learn

  • Sequence a short timed action with a coroutine instead of manual timers in Update.
  • Explain what a coroutine cannot easily answer that a named state machine can.
  • Choose between the two for an invented enemy attack sequence.

Target outcome

You can pick a coroutine or a state machine for a sequencing problem and explain the tradeoff out loud.

Visual walkthrough

Visual walkthrough

~6 min

  1. Two ways to sequence over time

    • Coroutine

      A method that can pause with yield and resume later, without blocking the main thread.

    • State machine

      An explicit set of named states with defined transitions between them.

    • Queryable state

      Being able to ask "what state is this object in right now" from outside the sequence.

  2. A short attack sequence as a coroutine

    Invented ranged-attack sequence. Each yield pauses the coroutine without freezing the rest of the game, and the method reads top-to-bottom like a script.

    Languagecsharp
    private IEnumerator FireSequence()
    {
        anim.Play("Charge");
        yield return new WaitForSeconds(chargeTime);
    
        anim.Play("Release");
        SpawnProjectile();
    
        yield return new WaitForSeconds(cooldownTime);
        canFire = true;
    }

    Code annotations

    1. Lines 1-3

      Wait without blocking

      WaitForSeconds pauses only this coroutine; other Update loops keep running.

      Effect: The charge animation plays for chargeTime seconds before the next line runs.

    2. Lines 5-6

      Act, then continue

      After the wait resolves, the sequence plays the release animation and spawns the projectile.

      Effect: The attack reads as one linear sequence, not a scattered set of timers.

  3. When each approach earns its cost

    ApproachOther systems can ask its stateHandles many branching statesSetup cost
    CoroutineGood for a short, mostly-linear sequence nobody else needs to query mid-flight.Hard - the sequence is a private control-flow script, not an inspectable state.Awkward once you need more than a couple of conditional branches.Low - a few lines, no extra structure.
    Explicit state machineGood when an animator, UI, or AI needs to know the current state at any moment.Easy - the current state is a named value other code can read.Scales cleanly to many states and transitions.Higher - requires defining states and transition rules up front.
  4. Pick the right tool

    A boss has five distinct combat states that the UI health bar, the music system, and the camera all need to react to differently. Coroutine or state machine?

    Expected reasoning

    State machine. Three separate systems need to query the boss's current state independently and react differently to each one - exactly the case a coroutine handles poorly, since its progress is private control flow rather than an inspectable value.

Go deeper