Unity Coroutine Not Working? StartCoroutine and StopCoroutine Fixed

Illustration: a looping glowing arrow cycle with a pause icon and a stopwatch

You call StartCoroutine(MyRoutine()) and nothing happens. Or the routine starts and you cannot stop it. Or it works, then freezes when you pause the game. Unity coroutine not working problems come from a small set of rules that are easy to miss: coroutines belong to the GameObject, they need it to be active, and StopCoroutine only works if you give it the right thing. This guide covers each cause with a fix, and when to skip coroutines altogether.

Quick Checklist

  1. Is the GameObject active in the hierarchy when you call StartCoroutine?
  2. Does the method return IEnumerator and do you call it with StartCoroutine(Method())?
  3. Does the routine contain at least one yield return?
  4. Are you stopping it with the same Coroutine reference you got from StartCoroutine?
  5. Is Time.timeScale 0 while you wait with WaitForSeconds?
  6. Does anything destroy or deactivate the object, or load a new scene?

Add a Debug.Log as the first line of the routine. If it never prints, the coroutine never started. If your script’s methods are not running at all, see the script not running guide.

How Coroutines Actually Work

A coroutine is a method that can pause and resume. Unity calls it again later, at the point you told it to wait for. It is not a thread. It runs on the main thread, in step with the frame loop.

using System.Collections;
using UnityEngine;

public class Flasher : MonoBehaviour
{
    void Start()
    {
        StartCoroutine(Flash());
    }

    IEnumerator Flash()
    {
        Debug.Log("Start");
        yield return new WaitForSeconds(1f);
        Debug.Log("One second later");
    }
}

Three details to remember. The method must return IEnumerator. You must add using System.Collections;. And calling Flash() by itself does nothing; it only builds the enumerator. StartCoroutine is what makes Unity run it. Writing Flash(); on its own line is the classic reason a coroutine “never runs”.

The Object Is Inactive or Destroyed

A coroutine is attached to the MonoBehaviour that started it. If the GameObject is inactive, Unity refuses and prints:

Coroutine couldn't be started because the the game object 'Enemy' is inactive!

Yes, “the the” is Unity’s real wording. This happens when you call StartCoroutine on a disabled object, or in the same frame you call SetActive(false).

  • Start it from an active object. A manager or the player can run the routine and operate on the inactive object.
  • Do not start coroutines on a prefab asset. Only on instances in the scene.
  • Disabling the script is different. Setting enabled = false on a MonoBehaviour does not stop its running coroutines. Deactivating the GameObject does.

Another trap: a routine that is running when you deactivate the object is stopped, and it will not resume when you reactivate it. You must start it again, for example in OnEnable.

void OnEnable()
{
    StartCoroutine(Pulse());
}

StopCoroutine Does Nothing

This is the most common coroutine bug after forgetting to call StartCoroutine. These lines look like they should work, but they do not:

StartCoroutine(Fade());
// later
StopCoroutine(Fade()); // does NOT stop the one that is running

Each call to Fade() creates a brand new enumerator. Stopping a fresh enumerator that was never started does nothing. Keep the reference that StartCoroutine returns instead:

Coroutine fadeRoutine;

void StartFade()
{
    if (fadeRoutine != null) StopCoroutine(fadeRoutine);
    fadeRoutine = StartCoroutine(Fade());
}

void StopFade()
{
    if (fadeRoutine != null)
    {
        StopCoroutine(fadeRoutine);
        fadeRoutine = null;
    }
}

This also fixes the second classic bug: starting the same routine twice, so two copies fight each other. Stopping the old one before starting a new one keeps only a single instance running.

Two other things to know:

  • The string form works too: StartCoroutine("Fade") and StopCoroutine("Fade"). It is slower, cannot take more than one parameter, and breaks silently if you rename the method. Prefer the Coroutine reference.
  • StopAllCoroutines() stops every coroutine started by that one MonoBehaviour. It does not touch coroutines started by other scripts.
  • A coroutine must be stopped by the same MonoBehaviour that started it. Calling StopCoroutine from a different script on a reference does not work.

Yield Mistakes

If the method has no yield at all, it is not a coroutine, and the compiler complains about the return value:

error CS0161: 'Flasher.Flash()': not all code paths return a value

The common yield instructions:

Yield Resumes
yield return null Next frame
new WaitForSeconds(t) After t seconds of scaled time
new WaitForSecondsRealtime(t) After t seconds of unscaled time
new WaitForFixedUpdate() After the next physics step
new WaitForEndOfFrame() After rendering, at the end of the frame
new WaitUntil(() => condition) When the condition becomes true
new WaitWhile(() => condition) When the condition becomes false

The most dangerous mistake is a loop with no yield inside it:

IEnumerator Wait()
{
    while (!ready)
    {
        // no yield here: this freezes the editor
    }
}

Without a yield, the loop never gives control back, and Unity hangs. Always yield inside a loop, even if only yield return null. Compare with a countdown that does it properly in the countdown timer guide.

A smaller mistake is writing yield return WaitForSeconds(1f) without new, which fails to compile.

WaitForSeconds and timeScale

WaitForSeconds counts scaled time. When Time.timeScale is 0, as in most pause menus, the wait never finishes. The routine looks frozen. The pause menu guide explains why timeScale pauses so much.

// Pauses with the game
yield return new WaitForSeconds(2f);

// Keeps counting while the game is paused
yield return new WaitForSecondsRealtime(2f);

Use WaitForSecondsRealtime for UI animations and menu delays that must work while paused. Use WaitForSeconds for gameplay that should freeze with the game. A slow motion effect with timeScale = 0.2f also stretches WaitForSeconds by five times, which may or may not be what you want.

What Stops a Coroutine for You

  • Destroying the GameObject or the MonoBehaviour. The routine is cancelled right away.
  • Deactivating the GameObject. Same result. It does not resume on reactivation.
  • Loading a new scene (non-additive). Objects in the old scene are destroyed, and their coroutines with them.
  • Exceptions. An unhandled exception ends that coroutine and prints an error. The rest of the game keeps running, which makes this easy to miss in a crowded Console.

If a coroutine must survive a scene load, start it on a persistent object. A simple approach is a manager object marked DontDestroyOnLoad. See the singleton patterns guide.

Also be careful with a coroutine that uses a destroyed object after a wait. If an enemy is destroyed during the wait, accessing it afterwards throws a MissingReferenceException. Check for null after each yield.

When Not to Use a Coroutine

Coroutines are great for short sequences: a flash, a fade, a delay before a spawn. They have limits. They cannot return a value, they cannot easily handle errors with try/catch around a yield, and they are tied to one MonoBehaviour. Unity 6 has Awaitable for async work with proper return values and cancellation. The Awaitable vs coroutines guide compares them.

For a simple delayed call, Invoke("Method", 2f) or a plain timer in Update are often simpler than a coroutine. If you only need to run some code once after a delay, a coroutine is more than you need.

📚 Learn the C# behind it

Coroutines are built on C# iterators. A solid C# book makes these rules feel obvious. See our picks of programming and game design books.

Troubleshooting

Coroutine couldn’t be started because the the game object is inactive!

The GameObject is disabled. Activate it first, or start the coroutine from a script on an active object.

The coroutine never runs and there is no error

You called the method without StartCoroutine. Use StartCoroutine(MyRoutine());.

StopCoroutine has no effect

You passed a new enumerator instead of the running one. Store the Coroutine returned by StartCoroutine and pass that.

Two copies of the coroutine run at once

The routine was started twice. Stop the stored reference before starting again, or guard with a bool.

The coroutine freezes when I pause

WaitForSeconds uses scaled time. Switch to WaitForSecondsRealtime for anything that must run during a pause.

Unity freezes when the coroutine starts

A while loop has no yield inside. Add yield return null;.

CS0161: not all code paths return a value

The IEnumerator method has no yield statement. Add one, or change the return type if it should not be a coroutine.

FAQ

Can a coroutine run on a non-MonoBehaviour class?

Not directly. StartCoroutine is a MonoBehaviour method. Pass in a MonoBehaviour to run it on, or use Awaitable.

Do coroutines run on another thread?

No. They run on the main thread, so a long loop without yields will block the game.

Can I wait for another coroutine to finish?

Yes. Use yield return StartCoroutine(Other()); and the outer routine resumes when the inner one ends.

Does a coroutine allocate memory?

Starting one allocates a little. Cache your WaitForSeconds objects if you create them in a tight loop, and avoid starting thousands per frame.

Why does my coroutine stop when I disable the script?

It does not. Disabling the script leaves coroutines running. Deactivating or destroying the GameObject stops them.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *