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

ConsoleRuntime exception · Scripting

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

StartCoroutine was called on a MonoBehaviour whose GameObject, or one of its parents, is inactive. Coroutines run as part of the object, so an inactive object cannot run one. Unity's message really does say "the the".

Read the Console line first

X is the inactive GameObject. Check it and every parent above it in the Hierarchy.

Common causes

  1. The object or a parent was deactivated

    A menu panel starts a fade coroutine on itself in the same frame it is hidden, or its parent canvas is inactive.

  2. A prefab is saved inactive

    Instances of an inactive prefab start inactive. Activate them before starting coroutines on them.

  3. The coroutine is started on someone else's behalf

    Calling otherScript.StartCoroutine runs it on otherScript's object, which may be the inactive one.

The fix

Fix
C#
// Start it on an object that stays active, such as your own manager singleton
UIManager.Instance.StartCoroutine(FadeOut(panel));

// Or activate first
panel.SetActive(true);
StartCoroutine(FadeIn(panel));

Stop it coming back

Run fades and timers that outlive a panel from a persistent manager, and stop coroutines in OnDisable when they should not survive the object.

Questions

Does disabling a component stop its coroutines?
No. Setting enabled = false on the MonoBehaviour leaves running coroutines going. Deactivating the GameObject, or destroying it, stops them all.
How do I run a coroutine for an object that is inactive?
Start it on an active MonoBehaviour, such as a manager that stays in the scene, and pass the inactive object in. The coroutine belongs to whichever component calls StartCoroutine.
Why does the message say 'the the'?
That is Unity's own wording; the typo is in the engine's message. Search for the rest of the text if you are looking it up.

Scripts that avoid it

Related errors

Got a different error? Paste it into the error lookup.