public async void LoadScene(int SceneIndex)
{
_LoaderCanvas.SetActive(true);
var Scene = SceneManager.LoadSceneAsync(SceneIndex);
Scene.allowSceneActivation = false;
do
{
Debug.Log(Scene.progress);
await Task.Delay(100);
} while (Scene.progress <= 0.9);
Scene.allowSceneActivation = true;
_LoaderCanvas.SetActive(false);
}
PS: when i say hang i mean, the scene is loaded, the code is running, things are happening like animations, but it will never continue to the next step
this hangs indefinitely, and never loads the next scene, until i click off of the unity play window, and click back on it. when i do that, it immediately loads the next scene. 0 clue why this happens.
the debug.log() returns 0 in the first frame, and then .9 in all subsequent frames.
also the await Task.delay is necessary. removing it causes the scene to actually hang forever, and crash unity
According to the manual for AsyncOperation (which the LoadSceneAsync returns):
“If you set allowSceneActivation to false, progress is halted at 0.9 until it is set to true.”
Your while loop is based on Scene.progress <= 0.9, so it will never exit the loop as the progress stays at 0.9 until allowSceneActivation is set to true (which you only do after exiting from your while loop).
If you’re checking it with your existing code, then that makes sense. Again, this is explained in the manual:
“When allowSceneActivation is set to false, Unity stops progress at 0.9, and maintains isDone at false.”
So if you wanted to use isDone then you would have your while loop checking that isDone is false, and then within that loop you would use an if check for progress being >= 0.9f and if it is then set allowSceneActivation to be true.
There is even an example that does exactly this in the manual (under allowSceneActivation):
The Async function is running on the main thread. If you don’t await it, it’s just a normal function that runs until it completes, so it would hang the program until your scene is loaded even if your loop logic worked. Yield is a better await condition in this case, because you want to check the progress every frame because you want things to happen as quickly as possible.