Search Unity

How to completely unload scene loaded additively?

Discussion in 'Editor & General Support' started by sergiusz308, Feb 17, 2020.

  1. sergiusz308

    sergiusz308

    Joined:
    Aug 23, 2016
    Posts:
    235
    I have main scene, into which I load additively another scene.

    That second scene I am unable to unloaded though using following code:

    Code (CSharp):
    1. SceneManager.UnloadSceneAsync(this.CurrentScene, UnloadSceneOptions.UnloadAllEmbeddedSceneObjects);
    this.CurrentScene is a reference to the loaded scene struct.

    Once unloading finished I also call Resources.UnloadUnusedAssets(); but it does not makes any difference, since whole hierarchy sits there like nothing happened.

    I have only one object marked with "DontDestroyOnLoad" - in the main scene. The loaded scene does not have any such objects.

    While in editor I am also unable to unload that scene - "Unload Scene" menu item is grayed out.

    How to completely get rid of it?

    2019.3.1f1.

    Thanks,,
    S.
     
  2. sergiusz308

    sergiusz308

    Joined:
    Aug 23, 2016
    Posts:
    235
    Well, for all out there who would encounter such problems in the future: the whole thing was my fault: I believe that this was the reason - instead of just using SceneManager.LoadSceneAsync and providing it with the name of the scene, I was creating new Scene first with SceneManager.CreateScene which returned an empty one, of the same name.

    Next I was calling LoadSceneAsync giving newly created scene as a parameter.

    As a result I got two scenes present at the same name - empty one, and the actual one, loaded up from the assets.

    Upon calling UnloadSceneAsync nothing was happening at all - I believe Unity did not expect to have two scenes of the same name, so did nothing or just failed silently, since no message was displayed.

    Anyway, I hate you Unity for being such a shy girl, when it comes to problems, caused by users ;-)