Adding delay between scene transitions (GameManager class does not inherit from MonoBehaviour)

I have a GameManager class which is a singleton, utility class. It controls scene transitions, scores, etc. It is a singleton because it holds global state that needs to shared across Scenes.

It does not inherit from MonoBehaviour so I can control its lifetime better (without resorting to DontDestroy… type functions).

I want to wait N seconds between scene transitions without blocking the UI (main) thread. However, all the way I know how to do such a thing in C# using TPL is coming up short because they end up calling the delegate (in this case, LoadNextLevel() function) from a different thread! Which Unity does not support / like :(.

Appreciate some help on how to accomplish this, thanks.

Debug.Log($"called from main thread: {System.Threading.Thread.CurrentThread.ManagedThreadId}");

Task.Delay(2000).ContinueWith(t =>
{
Debug.Log($"called from continueWith: {System.Threading.Thread.CurrentThread.ManagedThreadId}");
LoadNextLevel();
});

I don’t even know how to respond to that.

Just make the GameManager a singleton that derives from Monobehavior, then you can use a Coroutine: fade the old scene down, then load the new scene, then brighten up and delete the loading overlay.

Anytime you reach for System.Threading to do something extremely trivial in Unity you are almost certainly Doing It Wrong™.

Why do you want to avoid DontDestroyOnLoad?

Thanks for your responses. I agree - I am new to Unity and wasnt approaching this the right way (or rather - the right way to do this in Unity). I blame my years of experience in c# was interfering. Once I discovered how to do Singleton, MonoBehaviour-derived objects to hold my state, things are looking much cleaner now. I am indeed using DontDestroyOnLoad() now.

2 Likes