LoadSceneAsync never completes if allowSceneActivation is set to false

Hello,

I’m working on a project using Unity 2021.2.11f1 and I’m facing a weird issue where loading the scene asynchronously never completes if allowSceneActivation is set to false in the asyncOperation.

Here is my custom scene loading method

private IEnumerator LoadScene(string sceneName, LoadSceneMode loadMode, bool enableOnLoad = false)
        {
            //If the scene name is empty or if there is scene load already in progress, just exit
            if (sceneName.IsNullOrWhitespace() || _loadSceneOperation != null && !_loadSceneOperation.isDone)
                yield return null;

            //Load scene asynchronously
            _loadSceneOperation = SceneManager.LoadSceneAsync(sceneName, loadMode);

            //BUG: Currently setting allowSceneActivation to false causes the scene loading to get stuck and never call the OnSceneLoaded event
            _loadSceneOperation.allowSceneActivation = enableOnLoad;

            _loadSceneOperation.completed += SceneLoadingOperationCompleted;

            while (_loadSceneOperation != null && !_loadSceneOperation.isDone)
                yield return null;
        }

And here is how I call it

StartCoroutine(LoadScene(MainSceneName, LoadSceneMode.Additive));

The AsyncOperation never finishes and SceneManager.sceneLoaded event never gets triggered.

And in the editor the scene shows up as ‘is loading’ perpetually
192636-screenshot-2022-02-15-162948.png

If anyone has any idea, please advice!

In your while loop, you need to allow scene activation at 90%, or it will never activate.

//wait until the asynchronous scene fully loads
while (!_loadSceneOperation.isDone)
{
    //scene has loaded as much as possible, the last 10% can't be multi-threaded
    if (_loadSceneOperation.progress >= 0.9f)
    {
        //(here you can do other stuff, like custom loading or something)
        //then when done, you must call
        _loadSceneOperation.allowSceneActivation = true;
    }
    yield return null;
}