async.allowSceneActivation just isn't working (yesterday it was...)

Well, I know it’s not Unity- because I moved some things around and now things aren’t working as expected.

Thing is, the stuff I moved around was all outside my async level loading coroutine and not really relevant to it.

The problem is that the scene is loading immediately instead of halting. The code is originally Alan Zucconi’s (thanks Alan) but I do understand what every line should mean.

        private IEnumerator LoadNextScene(string targetScene)
        {
            AsyncOperation async = SceneManager.LoadSceneAsync(targetScene);
            async.allowSceneActivation = false;

            while (!async.isDone)
            {
                float progress = Mathf.Clamp01(async.progress / 0.9f);
                Debug.Log("Loading progress: " + (progress * 100) + "%");

                // Load complete
                if (async.progress == 0.9f)
                {
                    Debug.Log("Scene is ready for activation (any key)");
                    if (Input.anyKeyDown)
                    {

                    }
                }

                yield return null;
            }
        }

This should never activate the scene, but the scene is loaded and activated. (each Debug.Log is logged once)

Comparing floats to fixed values rarely works. Maybe make async.progress >= 0.9f

Also add a bool to the while statement to detect if the key is pressed and scene loading is done

Also you can remove the Mathf.Clamp if you make those changes

private IEnumerator LoadNextScene(string targetScene)
      {
         AsyncOperation async = SceneManager.LoadSceneAsync(targetScene);
          async.allowSceneActivation = false;
         bool done=false;
         while ( ( !async.isDone ) && ( !done ))
          {
              Debug.Log("Loading progress: " + (async.progress * 100) + "%");


              // Load complete
             if (async.progress >= 0.9f)
             {
                 Debug.Log("Scene is ready for activation (any key)");
                  if (Input.anyKeyDown)
                 {
                           done=true;
                           
                 }
             }


               yield return null;
         }

                     // activate scene here I guess

      }

Mmmm, thanks. I think I was interrupting something. I might have to get back to this thread. I was trying to load through to a loading scene, and provide it a target scene to load each time.

So I had this here, and it would start with a state machine change… But I was just loading a level before doing it; I think the previous load was not complete, breaking things.

Sorry to revive an old post. But I’m having the same problem.
I created a scene with just a background and a loading bar.
The idea was that when wanting to load scenes I would load this scene loading scene and it would load the desired scene.

The problem is that apparently the allowSceneActivation flag is ignored if you have recently loaded the scene, you need to wait a bit of time. Which is really odd.

I hope someone at unity sees this and takes a look.

Same issue on Unity 2019.1.6

yes can you report unity to fix this

for some reason , If you start SceneManager.LoadSceneAsync in Awake func or SceneManager.sceneLoaded callback
func, the allowSceneActivation won’t work.

the solution is start a coroutine and insert
yield return new WaitForSeconds(0.1f);
before loadsceneasync

something like

  IEnumerator CoSceneLoading(string sceneName)
    {
        yield return new WaitForSeconds(0.1f);
        var asynOp = SceneManager.LoadSceneAsync(sceneName);

        asynOp.allowSceneActivation = false;
       //do your work

}

find the answer from AsyncOperation.allowSceneActivation seems to be ignored - Questions & Answers - Unity Discussions

1 Like

The same Unity 2020.1.17f1

Thanks, save my day.
I put it in Awake and allowSceneActivation doesn’t work.