If I call
gameObject.active = false
from within my behavior script, does this automatically stop all running coroutines on the object?
If I call
gameObject.active = false
from within my behavior script, does this automatically stop all running coroutines on the object?
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.
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
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, …)
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.
@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.