- Home /
Pause a Coroutine from restarting on scene change?
Whenever I change a scene my coroutine resets and skips the 10 second wait I set it to. Is there a way I can make it where if the scene changes the coroutine will pause until it gets back to the same scene? Example: A 10 second coroutine waits 3 seconds, pauses on a different scene, when it returns the certain scene resumes the coroutine and then continues the remaining 7 seconds. Does anyone know a way of doing this? I currently have startcoroutine inside of the Update().
Answer by xxmariofer · Feb 03, 2019 at 11:28 PM
hello, coroutines will stop once the objects get destroyed, and all the objects get destroyed when you change of scene, you can use the dontdestroyonload for making sure the object is not getting destroyed, or having some sort of state machine for getting track of the coroutine state, the easiest solution i can think of, is using the OnDestroy method(similar at the Start but getting called when the object gets destroyed) and save the time since the start of the coroutine, so you can store the amount of seconds the coroutine was waiting, and in the start of the coroutine, if you are waiting for 10 secs for example wait for 10 - coroutineTimeElapse; for storing the coroutine time you can use playerprefs, or store it in a static var for getting track of it.
I added dontdestroyonload, but it still resets the coroutine. I deleted it since it causes the other game objects inside of the game object to go missing, which caused more errors.
well that depends of how it was done, the second approach should work for you.
Your answer
![](https://koobas.hobune.stream/wayback/20220612191039im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Pause and resume coroutine 0 Answers
Pause coroutine with infinite loop 1 Answer
Pause and resume coroutine 1 Answer
Is there a way to make WaitForSeconds ignore time.timescale? 1 Answer
How do I pause a method? 1 Answer