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. Dismiss Notice

Unity Hangs on SceneManager.UnloadScene()

Discussion in 'Editor & General Support' started by stevenbrent, Jan 17, 2016.

  1. stevenbrent

    stevenbrent

    Joined:
    Nov 22, 2015
    Posts:
    25
    (Version: Unity 5.3.0f4 Personal)

    I'm just getting started with Additive scene loading, and have hit an issue where Unity is hanging when I try to unload a scene with OnTriggerEnter or OnTriggerExit. The goal is to set up trigger colliders throughout my "Hub Room" which will load objects from other scenes or unload them if already loaded, when the player hits a trigger.

    The code to load objects from "Spoke Room 01" into my Hub room on trigger enter looks like this:

    Code (CSharp):
    1. void OnTriggerEnter(Collider other) { // Same result when using OnTriggerExit btw
    2.     if (other.gameObject.CompareTag("Player")) {
    3.        if (SceneManager.GetSceneByName("Spoke Room 01").isLoaded == false) {
    4.           // This works
    5.           SceneManager.LoadScene("Spoke Room 01", LoadSceneMode.Additive);
    6.             } else  {
    7.                 // This hangs Unity
    8.                 SceneManager.UnloadScene("Spoke Room 01");
    9.             }
    10.         }
    11.     }
    The new scene and its objects are loaded fine into the Hub on first collision with the trigger (and shows up in the Hierarchy at runtime, but Unity hangs (instead of unloading the new scene) if the new scene is already loaded and the player collides with the trigger. One thing I did to troubleshoot was use a keypress to manually unload the new scene, which worked fine. Any followup questions or suggestions for further troubleshooting are appreciated. Thanks!
     
    Last edited: Jan 17, 2016
  2. Jordi-Bonastre

    Jordi-Bonastre

    Unity Technologies

    Joined:
    Jul 6, 2015
    Posts:
    102
    Hi @stevenbrent , it seems to be a bug. I reported the problem to QA on behalf of you. The bug number is 762371.
     
    stevenbrent likes this.
  3. stevenbrent

    stevenbrent

    Joined:
    Nov 22, 2015
    Posts:
    25
    @Jordi Bonastre - thanks very much. In the meantime, I will see if I can find a workaround and will share anything useful I discover.
     
    Last edited: Jan 17, 2016
  4. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    I am thinking it might be worth trying to defer the loading and unloading and not do it inside a callback.
     
    Corvwyn and stevenbrent like this.
  5. stevenbrent

    stevenbrent

    Joined:
    Nov 22, 2015
    Posts:
    25
    Good call, @SteenLund -- I just tried this out and it seems to help. Level is loaded and unloaded without any issues. Thanks!

    Code (CSharp):
    1. void OnTriggerEnter(Collider other) {
    2.         if (other.gameObject.CompareTag("Player")) {
    3.             if (SceneManager.GetSceneByName("Spoke Room 01").isLoaded == false) {
    4.                 StartCoroutine("LoadSubScene");
    5.             } else if (SceneManager.GetSceneByName("Spoke Room 01").isLoaded == true) {
    6.                 StartCoroutine("UnloadSubScene");
    7.             }
    8.         }
    9. }
    10.  
    11. IEnumerator LoadSubScene() {
    12.         yield return new WaitForSeconds(.10f);
    13.         SceneManager.LoadScene("Spoke Room 01", LoadSceneMode.Additive);
    14. }
    15. IEnumerator UnloadSubScene() {
    16.         yield return new WaitForSeconds(.10f);
    17.         SceneManager.UnloadScene("Spoke Room 01");
    18. }
     
  6. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    That is good.

    I would still consider it a bug though, so we will make sure it works in the future
     
  7. James0oO

    James0oO

    Joined:
    Dec 8, 2014
    Posts:
    10
    Hello,

    Ive just been struggling with this too until I found this thread. Thanks for the workaround.
     
  8. Immanuel-Scholz

    Immanuel-Scholz

    Joined:
    Jun 8, 2013
    Posts:
    221
    I just reported an issue under #783271 with a crash in UnloadScene (that was loaded additive) instead of a hang and also in a coroutine (but which was started from a trigger callback).

    Not sure its the same error, but suspicious simillarities...
     
  9. TSRajesh

    TSRajesh

    Joined:
    Jun 19, 2013
    Posts:
    68
    Unfortunately, that doesn't work for me. I mean... It works in editor, but still crashing in standalone windows x86 exe.
     
  10. TobiUchiha

    TobiUchiha

    Joined:
    Jan 24, 2014
    Posts:
    21
    I can confirm that this works.
    I was trying to unload a scene OnTrigger, unity did nothing but hang.
    I used an Enumerator and waited for 0.1 second or so before making the call and it worked!

    Please fix the bug asap though.
     
  11. _watcher_

    _watcher_

    Joined:
    Nov 7, 2014
    Posts:
    259
    "Destroying GameObjects immediately is not permitted during physics trigger/contact or animation event callbacks"
    +1 would like it to be patched
     
    Last edited: Apr 27, 2016
  12. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    I am working on getting the fix released, but still can't tell you when it will land.

    You will be getting a new function UnloadSceneAsync which can be called at any time and I have made UnloadScene throw an exception if called in places where it would get stuck.
     
    McGravity, _watcher_ and stevenbrent like this.
  13. pixelsplit

    pixelsplit

    Joined:
    Sep 16, 2013
    Posts:
    171
    We are also having this problem with 5.4b19.
     
  14. Jkthrp

    Jkthrp

    Joined:
    Sep 18, 2013
    Posts:
    12
    Any news @SteenLund ? Still crashes in Unity 5.4.0f3
     
  15. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    @Jkthrp

    What crashes?
    The new UnloadSceneAsync has shipped and it is highly recommended you switch to this as it works under all circumstances.

    UnloadScene will now throw exception in those places where it is not allowed to be called. If you found a case where it still crashes when using UnloadScene please file a bug with a project that reproduces so I can analyze the script.
     
  16. Beardshaker

    Beardshaker

    Joined:
    Nov 3, 2014
    Posts:
    4
    To which Version? UnloadSceneAsync is not present in 5.4.1p1
     
  17. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    @Beardshaker

    Yes you are right, UnloadAsync is included in 5.5 which ships really soon, with the concurrent release we are doing I sometimes lose track of where changes are done :)

    So for now the prober way to Unload scenes are outside the various trigger callbacks as it will crash.
     
  18. acgleader

    acgleader

    Joined:
    Apr 17, 2013
    Posts:
    40
    What 5.4 version? Can you support UnloadAsync Also?
     
  19. enrgon

    enrgon

    Joined:
    Jul 24, 2017
    Posts:
    1
    The documentation doesn't mention anything about throwing exceptions so I would like to get some clarification. Are the following two statements are correct?
    UnloadScene() may throw an exception.
    UnloadSceneAsync() will not throw an exception.