Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

2018.2.3f1 Cannot Unload Previous Scene UnloadSceneAsync();

Discussion in 'Scripting' started by velitelvesmiru, Jul 3, 2019.

  1. velitelvesmiru

    velitelvesmiru

    Joined:
    Jun 26, 2019
    Posts:
    2
    I run into a problem where I am unable to Unload previous scene.
    I am building project in WebGL, all scenes do load perfectly, only problem comes when I am trying to unload previous scene, scene does NOT unload and then I browser runs into "out of memory" error message.

    Scenes are saved as AssetBundleds on server.
    Unity documentation says that I can use function SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single); - SceneManager should load and delete previous scene automatically, however that does not seem to do the job this time. Then I tried to load scene and then unload previous scene which name I stored as a string.
    SceneManager.LoadSceneAsync(sceneName)
    - > SceneManager.UnloadSceneAsync(toRemove);

    As well I used Resources.UnloadUnusedAssets(); which should work as a garbage collector.

    I have been trying to solve this for quite a while, after using more commands without any success I would like to ask if I someone can see any possible solution.

    Here is code which I use to load / unload scene. CallDownload is triggered when user clicks on button in menu to go to different level.



    Code (CSharp):
    1.  
    2. public void CallDownload()
    3.     {
    4.         StartCoroutine(Download());
    5.         string toRemove = SceneManager.GetActiveScene().name;
    6.     }
    7.  
    8.     IEnumerator Download()
    9.     {
    10.         Debug.Log("Please Wait");
    11.         AssetBundle assetBundle;
    12.        
    13.         string URL = "............./AssetBundles/" + assetName;
    14.         Debug.Log(URL);
    15.         //Download asset bundle
    16.         WWW bundleWWW = WWW.LoadFromCacheOrDownload(URL, 1);
    17.         yield return bundleWWW;
    18.         assetBundle = bundleWWW.assetBundle;
    19.         Debug.Log("50%");
    20.         string toRemove = SceneManager.GetActiveScene().name;
    21.         Debug.Log("toRemove = " + toRemove);
    22.         if (assetBundle.isStreamedSceneAssetBundle)
    23.         {
    24.             string[] scenePaths = assetBundle.GetAllScenePaths();
    25.             string sceneName = System.IO.Path.GetFileNameWithoutExtension(scenePaths[0]);
    26.             AsyncOperation load = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
    27.             yield return load;    
    28.             SceneManager.UnloadSceneAsync(toRemove);
    29.             string temp = sceneName;
    30.         }
    31.         Debug.Log("100%");
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    One possible problem is that coroutines don't survive scene loads. Does your "100%" log ever appear?
    Beyond that, I see that you are using LoadSceneMode.Single, which should unload all current scenes anyways, meaning you don't need to call UnloadSceneAsync. If you want to do things after a scene load (in Single mode, at least), you'll have to use the events in SceneManager, like sceneLoaded. https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-sceneLoaded.html
     
  3. velitelvesmiru

    velitelvesmiru

    Joined:
    Jun 26, 2019
    Posts:
    2
    100% does appear in first iteration, however, if the script is run again, then it print just some of the Debug.Log() commands, 100% is not in one of them. That could be a clue. Although I thought it'll be fine as scenes are loading just fine.
    Yes, I have read the documentation and Single Mode should really do that automatically, "UnloadSceneAsync" is there just out of desperation, as it does not remove scene either way.