Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
17
Question by CapnCromulent · Nov 16, 2010 at 07:03 AM · gameobjectcoroutineactive

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
26

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sh_code · Jan 13, 2019 at 11:08 PM -4
Share

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.

avatar image Bunny83 sh_code · Jan 13, 2019 at 11:41 PM 7
Share

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.


avatar image Bunny83 · Jan 13, 2019 at 11:34 PM 7
Share

$$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.

avatar image simon574 Bunny83 · Jan 14, 2019 at 12:33 AM 0
Share

@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.

avatar image
11

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IwishIcanFLighT · Dec 03, 2014 at 04:10 PM 0
Share

Worked like a charm for me. Thanks !

avatar image
8

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, ...)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nipunasudha · Oct 16, 2018 at 08:39 PM 0
Share

nice tip man

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jan 13, 2019 at 11:26 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Toggling a game object between an active and inactive state 1 Answer

Do something with GameObject with certain distance 1 Answer

Activate gameobject that is deactivated with c# 2 Answers

I want some gameobjects are all unactive! 1 Answer

How can I hide a GameObject without active=false? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges