• 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
6
Question by Essential · May 18, 2013 at 03:53 AM · gameobjectdisableenableactiveinvokerepeating

If you disable a gameobject, does an InvokeRepeating loop end or pause?

If you have a gameobject with a script running a current InvokeRepeating method and you set the gameobject to inactive, then back to to active, will the InvokeRepeating loop resume or will it have been cancelled outright?

Comment
Add comment · Show 1
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 Fattie · May 18, 2013 at 07:38 AM 0
Share

3 Replies

· Add your reply
  • Sort: 
avatar image
21
Best Answer

Answer by robertbu · May 18, 2013 at 06:16 AM

The answer is that the object will continue to execute the InvokeRepeating().

Deactivating a game object does not stop the InvokeRepeating().

To deactivate the whole game object: gameObject.SetActive(false)

Deactiving just the script component itself does not stop the InvokeRepeating().

to deactivate just the script component: t$$anonymous$$s.enabled = false

BUT NOTE THAT COROUTINES ARE DIFFERENT

Note however that ordinary coroutines, do indeed get stopped, when you deactivate the game object. However if you deactivate only script itself then the coroutine does keep going.

For InvokeRepeating...

1) disable whole game object - InvokeRepeating does not stop

2) disable just the script component - InvokeRepeating does not stop

For coroutines...

3) disable whole game object - coroutine DOES stop

4) disable just the script component - coroutine does not stop

In all four cases, the repeat is "eliminated", it is NOT paused. If you again SetActive(true), in all four cases, the repeat does NOT continue where it left off.

.

When I have questions like t$$anonymous$$s one, I just test it:

 private var i = 0;
 
 function Start() {
   InvokeRepeating("CountUp", 0.0, 0.5);
   Invoke("Disable", 3.0);
 }
 
 function Disable() {
     gameObject.SetActive(false);
 }
  
 function CountUp() {
     i = i+1;
     Debug.Log("i=" + i);
 }



to test all four situations, simply do t$$anonymous$$s,

 using UnityEngine;
 using System.Collections;
 public class CoTeste:MonoBehaviour
     {
     void Start ()
         {
         InvokeRepeating("_everySecondInvoked", 1.1f, 1.1f);
         StartCoroutine(_everySecondCoroutine());
         }
     private void _everySecondInvoked()
         {
         Debug.Log("t$$anonymous$$s I.R. appears every second "
             + Random.Range(1111111,9999999) );
         }
     private IEnumerator _everySecondCoroutine()
         {
         w$$anonymous$$le(true)
            {
            Debug.Log("\t\t\t\tt$$anonymous$$s coroutine appears every second "
               + Random.Range(1111111,9999999) );
            yield return new WaitForSeconds(Random.Range(1.1f,1.2f));
           }
         yield break;
         }
     }

Run the project, and simply in the editor try disabling just the script (un-tick the box in the inspector).

note that both the coroutine and the invokeRepating simply keep going forever.

Start over, and disable the whole game object (un-tick the box in the inspector).

note that the InvokeRepeating keeps going forever, BUT, the coroutine DOES STOP.


Simply use OnDisable()...

, or one of the similar routines, if you want somet$$anonymous$$ng "custom" to happen when a game object (or just the script) is disabled.

OnDisable and the similar routines

T$$anonymous$$s is completely commonplace. Indeed,

... You almost always use OnDisable() ... .

when you launch a coroutine or invokeRepeating - unless it's a really trivial situation and it doesn't matter.

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 Fattie · May 18, 2013 at 07:38 AM 0
Share
avatar image Essential · May 18, 2013 at 07:53 PM 1
Share
avatar image Fattie · Mar 19, 2015 at 08:18 AM 0
Share
avatar image Ash-Blue · Oct 09, 2017 at 11:56 PM 0
Share
avatar image
3

Answer by GingerLoaf · Mar 17, 2015 at 07:12 PM

T$$anonymous$$s post is very old at t$$anonymous$$s point, however I had some interesting thoughts that I wanted to share on t$$anonymous$$s subject for any future viewers.

  • always like to t$$anonymous$$nk about the enabled property of a component as somet$$anonymous$$ng closer to "allowUnityLifeCycleEvents" rather than enabled. An interesting example outside of the coroutine or invokerepeat case would be the case where I have (enabled) scriptA that references a disabled scriptB reference. ScriptA can invoke public facing members of scriptB despite the fact that scriptB is disabled. "Enabled" does not mean "not$$anonymous$$ng will function and everyt$$anonymous$$ng gracefully pauses".. instead it only stops the built in unity lifecycle events from being triggered (Awake, Start, Update, LateUpdate, ect...). A good test/sandbox could be t$$anonymous$$s:

    public class TestClassA : MonoBehaviour {

        private TestClassB m_classBReference = null;
    
          public void Update()
          {
               m_classBReference.Update();
          }
     
     }
     
     public class TestClassB : MonoBehaviour
     {
     
          public void Update()
          {
               Debug.Log("I am active!");
          }
     
     }
    
    
    1. Create a new scene, create a gameobject and place a "TestClassA" component on it

    2. Create another gameobject and place a "TestClassB" component on it

    3. Drag and drop the gameobject with your "TestClassB" component on it onto the "Class B Reference" property exposed in the inspector for the gameobject with the TestClassA component

    4. Run the scene

You should see the logs being printed out at first... now try disabling combinations of both scripts. If you disable TestClassB and leave TestClassA enabled, you should still see the logs. Play around with t$$anonymous$$s and become comfortable with how it works.

Now in regard to the "I t$$anonymous$$nk my script should disable everyt$$anonymous$$ng" mentality... Unity will not do t$$anonymous$$s for you, but you have powerful tools and frameworks at your fingertips to accomplish somet$$anonymous$$ng that you are comfortable with. You could create your own class to use instead of MonoBehaviour (pardon my awful programmer class name) named somet$$anonymous$$ng like "DisableableBehaviour". T$$anonymous$$s class can inherit from MonoBehaviour... then all you need to do is framework how you want it to work... for example: You could expose your own special function for starting coroutines or invokerepeat calls and have the class track them so that the class can cancel them on disable and restart them on enable. I'm certain there are tons of ways of doing t$$anonymous$$s... just know that unity doesn't need to do it all. Coroutines and invoke calls are very useful in the fact that they do span across frames regardless of enable or disable states, but you can always code up somet$$anonymous$$ng that works for you :)

Comment
Add comment · Show 2 · 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 Fattie · Mar 19, 2015 at 08:19 AM 0
Share
avatar image Fattie · Mar 19, 2015 at 08:26 AM -1
Share
avatar image
1

Answer by nitsnbolts_unity · Dec 18, 2020 at 05:38 AM

Not sure if t$$anonymous$$s was an option in Unity when t$$anonymous$$s was posted - but using Cancellnvoke() on any Monobehaviour script stops all invoke calls https://docs.unity3d.com/ScriptReference/MonoBehaviour.CancelInvoke.html

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Calling setActive(false) on a disabled object? 3 Answers

Bool activate/de-activate on timer C# 0 Answers

How to active/deactive gameobject? 1 Answer

Alternatives to GameObject.SetActive(...) 2 Answers

enabling / disabling child objects 1 Answer


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