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

Unload Current Scene

Discussion in 'Scripting' started by Black_Raptor, Jul 19, 2017.

  1. Black_Raptor

    Black_Raptor

    Joined:
    Nov 3, 2014
    Posts:
    181
    Hi,

    I try to find a way for unload the current scene when my player died, but that's don't work :(. This is my code :


    Code (CSharp):
    1. IEnumerator Wait_Score()
    2.         {
    3.             int y = SceneManager.GetActiveScene().buildIndex;
    4.             yield return new WaitForSeconds(Timer);
    5.             print("15 sec wait");  
    6.             SceneManager.UnloadSceneAsync(y);
    7.             SceneManager.LoadSceneAsync("Player_Load", LoadSceneMode.Additive);
    8.         }
    Thanks for help !
     
  2. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    356
    What's the problem? I can imagine you can't unload a scene when there's only one open scene. Maybe try putting Unload below Load.
    By the way - those Async functions return AsyncOperation. You might need to LoadAsync and use AsyncOperation.isDone to determine when you can Unload the other scene.
     
    Bovine likes this.
  3. Black_Raptor

    Black_Raptor

    Joined:
    Nov 3, 2014
    Posts:
    181
    I load all my scene in additive mode so Player_Load is always open, i load my over scene inside in additive
     
  4. Black_Raptor

    Black_Raptor

    Joined:
    Nov 3, 2014
    Posts:
    181
    Somone have an idea ?

    Code (CSharp):
    1. SceneManager.UnloadSceneAsync("Salle_Choix");
    2.                     SceneManager.LoadSceneAsync("Sea", LoadSceneMode.Additive);
    This one work perfectly so ... I think it's :

    Code (CSharp):
    1. int y = SceneManager.GetActiveScene().buildIndex;
    2.     SceneManager.UnloadSceneAsync(y);
    This don't work ?
     
  5. Black_Raptor

    Black_Raptor

    Joined:
    Nov 3, 2014
    Posts:
    181
    So i understand my problem, "y" return the actif scene but the problem is i load everything in additive mode so "y" return my active "Player_load" scene the one where i load in additive my other scene ...

    So now i try to SetActive the last scene loaded but that's not really work :

    Code (CSharp):
    1. public Transform destination;
    2.         private bool lastUsePressedState = false;
    3.         public string sceneToLoadStr;
    4.         Scene scene;
    5.  
    6.         private void OnTriggerStay(Collider collider)
    7.         {
    8.             var controller = (collider.GetComponent<VRTK_ControllerEvents>() ? collider.GetComponent<VRTK_ControllerEvents>() : collider.GetComponentInParent<VRTK_ControllerEvents>());
    9.             if (controller)
    10.             {
    11.                 if (lastUsePressedState == true && !controller.usePressed)
    12.                 {
    13.  
    14.                     StartCoroutine(Loading_Sea());          
    15.                
    16.  
    17.  
    18.                     var distance = Vector3.Distance(transform.position, destination.position);
    19.                     var controllerIndex = VRTK_DeviceFinder.GetControllerIndex(controller.gameObject);
    20.                     OnDestinationMarkerSet(SetDestinationMarkerEvent(distance, destination, new RaycastHit(), destination.position, controllerIndex));
    21.  
    22.                 }
    23.                 lastUsePressedState = controller.usePressed;
    24.             }
    25.         }
    26.    
    27.         IEnumerator Loading_Sea()
    28.         {
    29.             AsyncOperation loadNewScene = SceneManager.LoadSceneAsync(sceneToLoadStr, LoadSceneMode.Additive);
    30.             loadNewScene.allowSceneActivation = false;
    31.             scene = SceneManager.GetSceneByName(sceneToLoadStr);
    32.  
    33.             while (!loadNewScene.isDone)
    34.             {
    35.                 float progress = Mathf.Clamp01(loadNewScene.progress / 0.9f);
    36.  
    37.                 Debug.Log (loadNewScene.progress);
    38.  
    39.                 if (loadNewScene.progress == 0.9f)
    40.                 {
    41.  
    42.                     loadNewScene.allowSceneActivation = true;
    43.                     Debug.Log("now set scene, " + scene.name + " active");
    44.                     yield return new WaitForEndOfFrame();
    45.                     SceneManager.SetActiveScene(SceneManager.GetSceneByName(scene.name));
    46.                     SceneManager.UnloadSceneAsync("Salle_Choix");
    47.  
    48.                     yield return new WaitForSeconds(1);
    49.                     Debug.Log("active scene is, " + SceneManager.GetActiveScene().name);
    50.  
    51.  
    52.                     //Do your other stuff too.
    53.                 }
    54.  
    55.                 yield return null;
    56.             }
    57.         }
    But he always return "Player_Load" ... And not the scene i load
     
  6. Black_Raptor

    Black_Raptor

    Joined:
    Nov 3, 2014
    Posts:
    181
    Anybody ?
     
  7. DmitriyLeaf

    DmitriyLeaf

    Joined:
    Nov 3, 2018
    Posts:
    1
    I found working solution. Maybe it will be still relevant.

    Code (CSharp):
    1. // Get count of loaded Scenes and last index
    2. var lastSceneIndex = SceneManager.sceneCount - 1
    3.  
    4. // Get last Scene by index in all loaded Scenes
    5. var lastLoadedScene = SceneManager.GetSceneAt(lastSceneIndex)
    6.  
    7. // Unload Scene
    8. SceneManager.UnloadSceneAsync(lastLoadedScene);
    SceneManager.GetSceneAt() - Get the Scene at index in the SceneManager's list of loaded Scenes

    SceneManager.sceneCount - The total number of currently loaded Scenes
     
    sacb0y and Iresh like this.