How to not stop coroutines when deactivating a GameObject


We pause our game by deactivating all gameobjects in the scene and then displaying a menu. This works great, except that any coroutines running on those gameobjects are stopped. Is there a workaround for this short of basically writing my own coroutine system?

Also: what is the logic behind stopping coroutines when a gameobject is deactivated? Why would you want to make such a drastic state alteration on an object if you only want to temporarily disable it?

The answer is just disable the script, not the whole game object.

For InvokeRepeating…

  1. disable whole game object - InvokeRepeating does NOT stop

  2. disable just the script component - InvokeRepeating does NOT stop

For coroutines…

  1. disable whole game object - coroutine >>> DOES <<< stop

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

Simply use OnDisble(), OnEnable() and the similar routines if you want custom behaviour on disabling.

Details here.

Coroutines serve the object and are dependent of object life cycle. If your coroutine need to run without your object, the courotine is in the wrong place.

By the way. Normaly, you use Time.timeScale = 0 to pause a game. Coroutine and yield WaitForSecond respect this value.