Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by Deleted User, May 22, 2016.

  1. Deleted User

    Deleted User

    Guest

    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.

    Code (CSharp):
    1.         private IEnumerator LoadNextScene(string targetScene)
    2.         {
    3.             AsyncOperation async = SceneManager.LoadSceneAsync(targetScene);
    4.             async.allowSceneActivation = false;
    5.  
    6.             while (!async.isDone)
    7.             {
    8.                 float progress = Mathf.Clamp01(async.progress / 0.9f);
    9.                 Debug.Log("Loading progress: " + (progress * 100) + "%");
    10.  
    11.                 // Load complete
    12.                 if (async.progress == 0.9f)
    13.                 {
    14.                     Debug.Log("Scene is ready for activation (any key)");
    15.                     if (Input.anyKeyDown)
    16.                     {
    17.  
    18.                     }
    19.                 }
    20.  
    21.                 yield return null;
    22.             }
    23.         }
    This should never activate the scene, but the scene is loaded and activated. (each Debug.Log is logged once)

     
  2. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Comparing floats to fixed values rarely works. Maybe make async.progress >= 0.9f
     
  3. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Also add a bool to the while statement to detect if the key is pressed and scene loading is done
     
  4. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Also you can remove the Mathf.Clamp if you make those changes
     
  5. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Code (csharp):
    1. private IEnumerator LoadNextScene(string targetScene)
    2.       {
    3.          AsyncOperation async = SceneManager.LoadSceneAsync(targetScene);
    4.           async.allowSceneActivation = false;
    5.          bool done=false;
    6.          while ( ( !async.isDone ) && ( !done ))
    7.           {
    8.               Debug.Log("Loading progress: " + (async.progress * 100) + "%");
    9.  
    10.  
    11.               // Load complete
    12.              if (async.progress >= 0.9f)
    13.              {
    14.                  Debug.Log("Scene is ready for activation (any key)");
    15.                   if (Input.anyKeyDown)
    16.                  {
    17.                            done=true;
    18.                            
    19.                  }
    20.              }
    21.  
    22.  
    23.                yield return null;
    24.          }
    25.  
    26.                      // activate scene here I guess
    27.  
    28.       }
     
  6. Deleted User

    Deleted User

    Guest

    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.
     
  7. AgusB

    AgusB

    Joined:
    Jun 20, 2013
    Posts:
    65
    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.
     
  8. EarthHobbit

    EarthHobbit

    Joined:
    May 10, 2016
    Posts:
    41
    Same issue on Unity 2019.1.6
     
  9. CherstvyDmitriy

    CherstvyDmitriy

    Joined:
    Jul 19, 2017
    Posts:
    25
    yes can you report unity to fix this
     
  10. chucongqing

    chucongqing

    Joined:
    Jul 16, 2015
    Posts:
    2
    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
    Code (CSharp):
    1.   IEnumerator CoSceneLoading(string sceneName)
    2.     {
    3.         yield return new WaitForSeconds(0.1f);
    4.         var asynOp = SceneManager.LoadSceneAsync(sceneName);
    5.  
    6.         asynOp.allowSceneActivation = false;
    7.        //do your work
    8.  
    9. }
    find the answer from https://answers.unity.com/questions/1314424/asyncoperationallowsceneactivation-seems-to-be-ign.html
     
    Last edited: Sep 21, 2020
    Demo_101 likes this.
  11. CGDever

    CGDever

    Joined:
    Dec 17, 2014
    Posts:
    154
    The same Unity 2020.1.17f1
     
  12. hieutran196

    hieutran196

    Joined:
    Dec 17, 2018
    Posts:
    21
    Thanks, save my day.
    I put it in Awake and allowSceneActivation doesn't work.