- Home /
InvokeRepeating gets cancelled on scene change?
I use MonoBehaviour.InvokeRepeating to call a function every few seconds.
If I load the next scene, will the Invocation get cancelled automatically or I have to cancel it manually using MonoBehaviour.CancelInvoke?
(I haven't called Object.DontDestroyOnLoad on any object of the initial scene, I'm just wandering if invocations are going on in the background in the new scene, thus needlessly consuming resources)
I tried it (I put a Debug.Log line inside the invoked method) and it seems to get cancelled after a new scene is loaded.
Answer by Bunny83 · Aug 10, 2011 at 10:35 PM
If you look at the documentation of MonoBehaviour.InvokeRepeating you can see that InvokeRepeating is a member function of MonoBehaviour. Like coroutines (StartCoroutine()) those things run on the MonoBehaviour instance they has been started from. When the Object gets destroyed all coroutines or "InvokeRepeatings" are canceled immediately.
This is when most people would use hacky Unity techniques like GameObject.DontDestroyOnLoad(this.gameObject) to keep that GO alive across scenes. It's things like this that makes Unity a pain to work with.