Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

SceneManager.UnloadSceneAsync returns null

Discussion in 'Scripting' started by PatrickReynolds, May 25, 2017.

  1. PatrickReynolds

    PatrickReynolds

    Joined:
    Nov 19, 2014
    Posts:
    15
    Whenever I attempt to unload a scene using the scene manager, the AsyncCoroutine gets returned as null, so I can't track its progress. Here's the code I'm using to try to unload the scene:

    Code (CSharp):
    1.  
    2.     private AsyncOperation asyncUnload;     //The unload Async Operation
    3.  
    4.  
    5.     ....
    6.         //Start the unload coroutine, pass in name of the scene I'm trying to unload.
    7.         StartCoroutine(UnloadLevel(SceneManager.GetActiveScene().name));
    8.     ....
    9.  
    10.     private IEnumerator UnloadLevel(string levelName)
    11.     {
    12.         //asyncUnload = SceneManager.UnloadSceneAsync(levelName);
    13.         //asyncUnload = SceneManager.UnloadSceneAsync(1);
    14.         asyncUnload = SceneManager.UnloadSceneAsync(SceneManager.GetActiveScene());
    15.        
    16.         Debug.Log(asyncUnload);
    17.  
    18.         isSceneUnloaded = false;
    19.  
    20.         while (asyncUnload.progress < .9)
    21.         {
    22.             yield return null;
    23.         }
    24.  
    25.         yield break;
    26.     }
    Whenever it gets to the debug output, asyncUnload is always null. I've tried all three variations of the method, double checked that I'm passing in the correct name, and made sure that the scene I'm currently trying to unload is always index 1. No matter what, it always returns null, so I can't use the while loop to track its progress.

    I'm sure I'm just missing something simple, but I'm pretty perplexed as to why this isn't working.


    Thanks for any help you can offer.
     
    Last edited: May 25, 2017
  2. KhenaB

    KhenaB

    Joined:
    Aug 21, 2014
    Posts:
    260
    Same issue here, trying to figure out the new UnloadSceneAsync to replace UnloadScene and i'm having tons of issues. It's almost impossible to get help with scene management, i'm considering staying with UnloadScene and never upgrading Unity again for the duration of the development on my game.
     
  3. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    726
    I see you set your thread to "Solved" please keep any information that existed there and please post your fix in the comments. We cannot learn if no one teaches. Show us how to fix it, as you would expect of others to show solutions aswell, its the point of the forums.
     
  4. KhenaB

    KhenaB

    Joined:
    Aug 21, 2014
    Posts:
    260
    I've set my thread to solved because i just gave up and went with a hacky solution that works with my game's design, i don't think it is something people would have benefited from and it's impossible to delete threads.

    Since unloading the current scene completely before activating the next scene seems impossible all i did was loop through all the game objects in the current scene after the screen has faded out and disable them all to prevent them from colliding with the next scene. It works for now but having to do this doesn't feel right, I just don't have time to deal with the issue right now.

    Code (CSharp):
    1. foreach(GameObject go in SceneManager.GetActiveScene().GetRootGameObjects())
    2.         {
    3.             go.SetActive(false);
    4.         }
     
    TaleOf4Gamers likes this.
  5. _watcher_

    _watcher_

    Joined:
    Nov 7, 2014
    Posts:
    84
    Last edited: Jul 13, 2017
  6. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,130
    @_watcher_ Could you update your Unity Answer link?
     
  7. vestigial

    vestigial

    Joined:
    May 9, 2015
    Posts:
    16
    I ran into this, it turned out I was trying to unload the last remaining scene. I guess you have to always have at least one scene active. I got around it by making a new scene with SceneManager.CreateScene() just before calling delete on the existing ones.
     
    IgorAherne likes this.
  8. LucianoMacaDonati

    LucianoMacaDonati

    Joined:
    Dec 31, 2012
    Posts:
    7
    Thank you!!!