Search Unity

2017.1.0 - SceneManager.AddSceneAsync cannot load more than one additive scene

Discussion in 'Editor & General Support' started by Yodzilla, Jul 25, 2018.

  1. Yodzilla

    Yodzilla

    Joined:
    Mar 21, 2013
    Posts:
    48
    In my project we have one main scene that we load other mini sub-scenes in and out of. We're managing which of these are loaded and unload them when finished. I wanted to clean up the loading screen a bit and found a weird bug in that using LoadSceneAsync in Additive mode while manipulating allowSceneActivation only works once. After the first scene is loaded (and unloaded) calling this function again will get to "async.allowSceneActivation = true" but then nothing happens, the scene is never loaded and everything just stops.

    Code (CSharp):
    1. private IEnumerator AddSceneAsync(string sceneName)
    2. {
    3.     AsyncOperation async = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
    4.     async.allowSceneActivation = false;
    5.  
    6.     _loadingPercentage = 0;
    7.  
    8.     while (!async.isDone)
    9.     {
    10.         _loadingPercentage = async.progress;
    11.        
    12.         if (async.progress >= 0.9f)
    13.         {
    14.             _loadingPercentage = 1f;
    15.  
    16.             yield return new WaitForSeconds(1);
    17.  
    18.             async.allowSceneActivation = true;
    19.         }
    20.  
    21.         yield return null;
    22.     }
    23. }
    What's going on here? Is this a bug in Unity or some weird limitation of LoadSceneAsync? It's like calling it once and delaying scene activation is dirtying up the SceneManager in some way but no errors or warnings are ever thrown. If I don't ever set allowSceneActivation to false it works just fine as many times as I want. Why?
     
  2. Yodzilla

    Yodzilla

    Joined:
    Mar 21, 2013
    Posts:
    48
    Got pulled off into something else but came back to this. So if I replace the WaitForSeconds line with

    Code (CSharp):
    1. while (!Input.GetKeyDown(KeyCode.Space))
    2. {
    3.     yield return null;
    4. }
    It works just fine. Any sort of while there works fine but any sort of yield causes the async operation to silently break but ONLY after the first time its run. The first scene that loads with a yield there works, every attempt after fails. What a mystery.
     
  3. Yodzilla

    Yodzilla

    Joined:
    Mar 21, 2013
    Posts:
    48
    Got this figured out thanks to Twitter user EthanF4D. For some reason the process of loading an additive scene asynchronously is setting Time.timeScale to 0 but ONLY for second and later loads. It's set to 1 the first time and then something about the loading process is pausing time when called again. Setting Time.timeScale to 1 after checking if async.progress is above 0.9 fixes it!
     
    hopeful likes this.