Search Unity

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

Discussion in 'Scripting' started by clearoutlines, May 22, 2016.

  1. clearoutlines

    clearoutlines

    Joined:
    Feb 2, 2016
    Posts:
    145
    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. clearoutlines

    clearoutlines

    Joined:
    Feb 2, 2016
    Posts:
    145
    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:
    22
    Same issue on Unity 2019.1.6
     
  9. CherstvyDmitriy

    CherstvyDmitriy

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