Application.WaitForAsyncOperationToComplete() causes game-freeze in Editor

I’m creating a pause menu for my game and I have two scenes: a menu scene (Scene1) and a game scene (Scene2). I load them using the command ‘SceneManager.LoadScene(1);’ for the menu scene and ‘SceneManager.LoadScene(2);’ for the game scene.

The game mission opens successfully when I transition from the menu scene to the game scene for the first time. However, if I pause the game and return to the menu scene, and then try to go back to the game scene, the game freezes. This problem also occurs in the build version of the game.

Upon deep profiling, I found that the second ‘WaitForAsyncOperationToComplete()’ call is making expensive calls. I have tried minimizing my ‘Start()’ and ‘Awake()’ methods, but nothing seems to work.

I’ve attached some screenshots


You might need to show some code samples of what you’re trying to do here.

Upon further investigation, I have identified the error in my code. The issue arises when I navigate back to the Main Menu, as I mistakenly set the TimeScale to 0. I was unaware that Unity retains the TimeScale value even when transitioning between scenes. Initially, I assumed that Unity would reset the time scale whenever I returned to the Main Menu.

1 Like

To add to this, this is true of any static values. They exist independant of scenes/assets, so they persist between scene reloads. Something to keep in mind.

1 Like

While you already solved your actual bug involving Time.timeScale, I’d like to add an important point for anyone else looking at this: asynchronous operations, at least those for loading or unloading Scenes asynchronously always act synchronously in Editor Play mode. It’s a specific implementation for Editor stuff and apparently cannot be circumvented: whenever you async-load-or-unload a Scene in Editor the game will freeze on that frame for a longer hitch, then carry on fully completed the very next frame.

For example this means that you cannot make loading screen animations and such that would work in Editor Play mode - they will be completely fine in built Player in the end, though. Always test and confirm both in Editor and build when dealing with Scene loading (I pass by here looking for answers for a separate problem possibly involving Scene loading, too).

SceneManager.LoadScene by design freezes the whole main thread until the scene has finished loading. This is what its documentation says:

Note: In most cases, to avoid pauses or performance hiccups while loading, you should use the asynchronous version of this command which is: LoadSceneAsync.