Unload Current Scene

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 :

IEnumerator Wait_Score()
        {
            int y = SceneManager.GetActiveScene().buildIndex;
            yield return new WaitForSeconds(Timer);
            print("15 sec wait");  
            SceneManager.UnloadSceneAsync(y);
            SceneManager.LoadSceneAsync("Player_Load", LoadSceneMode.Additive);
        }

Thanks for help !

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.

1 Like

I load all my scene in additive mode so Player_Load is always open, i load my over scene inside in additive

Somone have an idea ?

SceneManager.UnloadSceneAsync("Salle_Choix");
                    SceneManager.LoadSceneAsync("Sea", LoadSceneMode.Additive);

This one work perfectly so … I think it’s :

int y = SceneManager.GetActiveScene().buildIndex;
    SceneManager.UnloadSceneAsync(y);

This don’t work ?

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 :

public Transform destination;
        private bool lastUsePressedState = false;
        public string sceneToLoadStr;
        Scene scene;

        private void OnTriggerStay(Collider collider)
        {
            var controller = (collider.GetComponent<VRTK_ControllerEvents>() ? collider.GetComponent<VRTK_ControllerEvents>() : collider.GetComponentInParent<VRTK_ControllerEvents>());
            if (controller)
            {
                if (lastUsePressedState == true && !controller.usePressed)
                {

                    StartCoroutine(Loading_Sea());           
               


                    var distance = Vector3.Distance(transform.position, destination.position);
                    var controllerIndex = VRTK_DeviceFinder.GetControllerIndex(controller.gameObject);
                    OnDestinationMarkerSet(SetDestinationMarkerEvent(distance, destination, new RaycastHit(), destination.position, controllerIndex));

                }
                lastUsePressedState = controller.usePressed;
            }
        }
   
        IEnumerator Loading_Sea()
        {
            AsyncOperation loadNewScene = SceneManager.LoadSceneAsync(sceneToLoadStr, LoadSceneMode.Additive);
            loadNewScene.allowSceneActivation = false;
            scene = SceneManager.GetSceneByName(sceneToLoadStr);

            while (!loadNewScene.isDone)
            {
                float progress = Mathf.Clamp01(loadNewScene.progress / 0.9f);

                Debug.Log (loadNewScene.progress);

                if (loadNewScene.progress == 0.9f)
                {

                    loadNewScene.allowSceneActivation = true;
                    Debug.Log("now set scene, " + scene.name + " active");
                    yield return new WaitForEndOfFrame();
                    SceneManager.SetActiveScene(SceneManager.GetSceneByName(scene.name));
                    SceneManager.UnloadSceneAsync("Salle_Choix");

                    yield return new WaitForSeconds(1);
                    Debug.Log("active scene is, " + SceneManager.GetActiveScene().name);


                    //Do your other stuff too.
                }

                yield return null;
            }
        }

But he always return “Player_Load” … And not the scene i load

Anybody ?

I found working solution. Maybe it will be still relevant.

// Get count of loaded Scenes and last index
var lastSceneIndex = SceneManager.sceneCount - 1

// Get last Scene by index in all loaded Scenes
var lastLoadedScene = SceneManager.GetSceneAt(lastSceneIndex)

// Unload Scene
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

3 Likes