Search Unity

Loading and Unloading scenes

Discussion in 'Scripting' started by lovemoebius, Mar 15, 2017.

Thread Status:
Not open for further replies.
  1. lovemoebius

    lovemoebius

    Joined:
    Dec 12, 2016
    Posts:
    88
    I've recently switched from the old, obsolete, Application.LoadLevel to SceneManager.LoadScene.
    Now, while the old method loaded a new scene without any problems, SceneManager.LoadScene just loads the new scene on top of the old one.

    So I went and looked if there was a way to unload the current scene and found UnloadSceneAsync, which seems to be what I need, except it does absolutely nothing when called.

    Code (CSharp):
    1.  
    2.         if (Input.GetKeyDown(KeyCode.Escape)){
    3.             SceneManager.LoadScene ("mainScene", LoadSceneMode.Additive);
    4.             SceneManager.UnloadSceneAsync ("machine");
    5.     }
    This is the bit of my code that is supposed to load and unload, it doesn't seem to be wrong.

    Any idea what's going on? How do I unload my old scene?
     
    FiveFingerStudios likes this.
  2. dutchkiller2000

    dutchkiller2000

    Joined:
    May 13, 2016
    Posts:
    121
    if you load a scene the other scene will unload automaticly
     
  3. lovemoebius

    lovemoebius

    Joined:
    Dec 12, 2016
    Posts:
    88
    Not for me...
     
    sonictimm and hamik166 like this.
  4. donivanbardin

    donivanbardin

    Joined:
    Mar 15, 2017
    Posts:
    9
    Try this one out:

    Application.LoadLevel("Area 2");

    add Area 2 to the build settings first and it will load. According to the document I linked it destroys the old scene before loading the new one so it should hopefully solve your problem!
    https://docs.unity3d.com/ScriptReference/Application.LoadLevel.html

    The method is obsolete but I just tested it and it worked for me. Let me know if it works!
     
  5. lovemoebius

    lovemoebius

    Joined:
    Dec 12, 2016
    Posts:
    88
    It does work but the lighting looks strange, darker than it should.
    Also should I be using something that is obsolete?
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    *facepalm*

    Is the scene named machine? Have you tried using the build index or the actual reference to the scene (assuming you loaded it via code)? If you grab the AsyncOperation this method returns and monitor it does it ever report completing?
     
    catblue44 likes this.
  7. lovemoebius

    lovemoebius

    Joined:
    Dec 12, 2016
    Posts:
    88
    Scene called machine exists and is in the build settings.
    Neither build index (if that is the number next to the scene in the build settings) nor a reference make any difference.

    I'm also not entirely sure how to use AsyncOperation
     
  8. donivanbardin

    donivanbardin

    Joined:
    Mar 15, 2017
    Posts:
    9
  9. lovemoebius

    lovemoebius

    Joined:
    Dec 12, 2016
    Posts:
    88
    I just noticed I'm using the wrong Mode to load the scene, instead of Single I'm using Addittive which is exactly what's causing the problem.

    Sorry for wasting everybody's time
     
  10. Deleted User

    Deleted User

    Guest

    I would not say that it's an actual problem. The problem is that you need to wait until the second scene has finished loading.
    In case of regular loading this means yielding a frame:

    Code (CSharp):
    1.     void Update ()
    2.     {
    3.         if (Input.GetKeyDown(KeyCode.Escape))
    4.         {
    5.             StartCoroutine(SceneSwitch);
    6.         }
    7.     }
    8.  
    9.     IEnumerator SceneSwitch()
    10.     {
    11.         SceneManager.LoadScene("mainScene", LoadSceneMode.Additive);
    12.         yield return null;
    13.         SceneManager.UnloadSceneAsync("machine");
    14.     }
    In the async case it means waiting for the async operation to be finished

    Code (CSharp):
    1.     void Update ()
    2.     {
    3.         if (Input.GetKeyDown(KeyCode.Escape))
    4.         {
    5.             StartCoroutine(SceneSwitch);
    6.         }
    7.     }
    8.  
    9.     IEnumerator SceneSwitch()
    10.     {
    11.         AsyncOperation load = SceneManager.LoadSceneAsync("mainScene", LoadSceneMode.Additive);
    12.         yield return load;
    13.         SceneManager.UnloadSceneAsync("machine");
    14.     }
     
    RenanRL, skinwalker, ksf000 and 2 others like this.
  11. sherkhan54111_unity

    sherkhan54111_unity

    Joined:
    Oct 11, 2022
    Posts:
    1
    no thats no true
     
    sonictimm likes this.
Thread Status:
Not open for further replies.