- Home /
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?
Answer by simon574 · Jan 13, 2019 at 08:55 PM
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." https://docs.unity3d.com/2017.4/Documentation/Manual/Coroutines.html
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 ins$$anonymous$$d of lazily posting a stupid question.
Sorry, but your contribution here is actually not helpful at all. As you can see several people had asked themselfs the same question. Why did you actually end up here? The point of UnityAnswers is to provide a knowledge base for the whole community.
$$anonymous$$any people try something out for themselfs, interpret the results wrong and draw false conclusions (like in this answer for example). You downvoted a valid question but upvoted a an answer which comes to a wrong conclusion.
$$anonymous$$onoBehaviours do not have a SetActive method, only the gameobject has. Any yes, coroutines do get stopped when the gameobject is deactivated. The paragraph you read was talking about the enabled state of the $$anonymous$$onoBehaviour, not the active state of the owning gameobject.
A deactivated gameobject is essentially "taken out of existance" from a scenes perspective. Deactivated gameobjects do not receive any callbacks, aren't ticked or considered in (almost) any search functions. Disabling a component on the other hand will just switch the main functionality of that component off. For $$anonymous$$onoBehaviours that means Update, FixedUpdate, LateUpdate won't be called anymore. However other callbacks like OnBecameVisible will still be called.
So you did not read the documentation carefully and mixed up the enabled state of a single component with the active state of the gameobject.
@Bunny83 thanks for the clarification. I thought I've read the documentation carefully. It nowhere mentions that the game object ter$$anonymous$$ates all co-routines when it gets deactivated. There is a section "Deactivating GameObjects", it does not say anything about co-routines either.
Answer by Rafes · Aug 01, 2011 at 12:37 AM
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
Answer by KruegerT · Dec 03, 2015 at 12:56 PM
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, ...)
Answer by badadam · Jan 13, 2019 at 10:31 PM
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 "$$anonymous$$onoBehaviour 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 $$anonymous$$onoBehaviour. So a coroutine runs on the $$anonymous$$onoBehaviour that was used to call StartCoroutine.
Note that this question is already more than 8 years old.