I have a very bad memory of a 3-month old bug in Lua that was caused by a timer that was not cancelled upon level change and was wondering about the same thing in Unity.
For something in the game that needs to happen after specific amount of times, I use coroutines and I’m curious to know what happens if I schedule a for example function call to happen in 60 seconds from now but I change the scene immediately?
What happens to that scheduled function call? Will it get canceled/destroyed or we need to manually cancel them ourselves.
I believe that coroutines are always attached to the MonoBehavior gameObjects holding their code. They aren’t really separate processes, which can zombie. If the gameObject is destroyed (which a scene change would do,) the coroutine dies with it.
Coroutine calls are tied to the calling MonoBehaviour, not the MonoBehaviour they actual code is on. Classic rookie mistake is to have a bullet call a coroutine on its target immediately before destroying the bullet. Even though the target continues to exist the coroutine stops execution.
There are multiple opinions as to what happens to these orphan coroutines once they are cut off from the world. Some people claim that they go on to a better place, where they can run however they like, free from the constraints of their yield statements. Others claim that they simply cease to exist the moment their GameObject is destroyed. Yet a third opinion is they hang around in memory, in a limbo of sorts, until there souls are claimed by the garbage collector.
Note: If you are unsure of any functionality the best thing is to open up am empty projects and test it in isolation.