Does deactivating a GameObject automatically stop its coroutines?

If I call

gameObject.active = false

from within my behavior script, does this automatically stop all running coroutines on the object?

6 Answers

6

From the Unity docs: “Note: Coroutines are not stopped when a MonoBehaviour is disabled, but only when it is definitely destroyed. You can stop a Coroutine using MonoBehaviour.StopCoroutine and MonoBehaviour.StopAllCoroutines. Coroutines are also stopped when the MonoBehaviour is destroyed.”

It’s not true, though. I’ve tried this and for me the coroutines are stopped as soon as I call SetActive(false) on the MonoBehaviour.
EDIT: As @Bunny83 has pointed out, SetActive() disables the GameObject, which is not the same as disabling the MonoBehaviour. I still find it very misleading that the documentation never mentions that disabling a GameObject also stops all coroutines. They do not continue when you re-enable the object.

upvote for trying it yourself, which is what the asker of the question should have done in the first place instead of lazily posting a stupid question.

–

Dont be a crybaby, everyone learns at their own pace. Nobody likes a gatekeeping nerd.

–

Yes (as noted already)

You can use OnEnable() to start the co-routine back up if needed:
http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnEnable.html

Worked like a charm for me. Thanks !

–

Thank you, great idea!

–

Just one note:

If you disable a GameObject, the Coroutine on the object will still execute its own code until the end/the next yield!

This is important, for example, if the script disable itself, but afterwards (within the same function) enable something else (allow interaction again, let another animation play, …)

nice tip man

–

Coroutines are run by MonoBehaviour object which include them. If the gameobject which has the MonoBehaviour class script as a component become inactive or is destroyed all coroutines in MonoBehaviour object are destroyed. So all of them stop.

It's not clear what you mean by "MonoBehaviour object which include them". If you mean that the coroutine runs on the monobehaviour where they are declared in, this is not true. A coroutine doesn't need to be defined inside a monobehaviour. However you need a monobehaviour instance since the StartCoroutine method is an instance method of MonoBehaviour. So a coroutine runs on the MonoBehaviour that was used to call StartCoroutine. Note that this question is already more than 8 years old.

–

@CapnCromulent
Yes

Note: You can stop a coroutine using
MonoBehaviour.StopCoroutine and
MonoBehaviour.StopAllCoroutines.
Coroutines are also stopped when the
MonoBehaviour is destroyed or if the
GameObject the MonoBehaviour is
attached to is disabled
. Coroutines
are not stopped when a MonoBehaviour
is disabled.

unity docs

I found some mixed answers in this thread, so since I also wanted to know the answer to this question, after 16 years… (kudos to Unity for keeping the posts opened), I tested it myself.

With the following piece of code, the screen (a monitor that I wanted to turn off after a pause, after playing a minigame) is not turned off, and I read in the console only “disabling…”. Meaning that the “gameObject.SetActive(false)” is executed before the 2 seconds of pause and the coroutine is stopped.

    void Update()
    {
        if (playerController.enabled)
        {
        StartCoroutine(TurnOffScreen());
        gameObject.SetActive(false);
        }
    }

    IEnumerator TurnOffScreen()
    {
        print("disabling...");
        yield return new WaitForSeconds(2f);
        screenOff.SetActive(true);
        screenOn.SetActive(false);
        print("done!");
    }

With this change (just to prove that the instructions screenOff/On were correct), instead, the screen is turned off, but I don’t read “done!”.

    IEnumerator TurnOffScreen()
    {
        print("disabling...");
        screenOff.SetActive(true);
        screenOn.SetActive(false);
        yield return new WaitForSeconds(2f);
        print("done!");
    }

So yeah, as someone said in this thread, disabling the gameObject that contains the coroutine, stops also the coroutine.

The whole code makes no sense, I know (there is no pause in the second version, so the coroutine is useless written in that way). It’s just an example to check if it was running or not.