Get Previous Scene Index number using SceneManager

Interesting question earlier over on Discord.

How to find the previous scene that was loaded and have that stored.

Since OnLevelWasLoaded() has now been depracated, I needed to find an alternative using SceneManagement.

Now I know there’s many ways to skin a cat, but this is just what I came up with this morning :slight_smile:

So I have a persistent GameManager script or the like in the game and ive just added this to it.


remember to add this namespace at the top of the script tho

using UnityEngine.SceneManagement;

I just added this to a persistent game manager script

        // we can use the SceneUnloaded delegate of scenemanager to listen for scenes that have been unloaded
        void OnEnable()        {    SceneManager.sceneUnloaded += SceneUnloadedMethod;        }
        void OnDisable()        {    SceneManager.sceneUnloaded -= SceneUnloadedMethod;        }
    
        int lastSceneIndex = -1;

        // looks a bit funky but the method signature must match the scenemanager delegate signature
        void SceneUnloadedMethod (Scene sceneNumber)
        {
                int sceneIndex = sceneNumber.buildIndex;
                // only want to update last scene unloaded if were not just reloading the current scene
                if(lastSceneIndex != sceneIndex)
                {
                        lastSceneIndex = sceneIndex;
                        Debug.Log("unloaded scene is : " + lastSceneIndex);
                }
        }
        public int GetLastSceneNumber()
        {
                return lastSceneIndex;
        }

Ive just stuck in a public method there at the bottom so any external script can get a hold of that scene index if needed.

like I said, there will be a more elegant way of getting this, but this is all I could think of this morning, I found it an interesting question so thought I would share what I came up with and hope it helps someone.

2 Likes

Hey Daz,

Looks great, nice one…

One question, regarding its usage, SceneUnloadedMethod() takes a scene as a parameter, yet the only piece of information you require is the buildIndex from it. Why not just pass in an int?

Yeah @Rob would have been the ideal, but as far as i could see the Scenemanager has a few events for Scene stuff rather than tying in MonoBehaviours like they used to do.

the only implementation events for the scenemanager that i could see were

UnityAction<Scene, Scene> activeSceneChanged;
UnityAction<Scene, LoadSceneMode> sceneLoaded;
UnityAction sceneUnloaded;

and looking at the UnityEngine assembly these were in there


       [RequiredByNativeCode]
		private static void Internal_SceneUnloaded (Scene scene); 



	public static event UnityAction<Scene> sceneUnloaded {
			add;
			remove;
		}

so I had to tie in the Delegate subscription to match the sig, and Scene was what it needed.

dont know if theres any other way as its all i could find, but it does seem like a long winded way of going about it.

Bit over my head to get too deep lol, dont understand it

1 Like

Gotcha…

I was watching a YouTube clip the other day from the Unite 2017 conference, it mentioned some limitation specifically and then they went on to explain how you could get around it by using the Unity Internals instead… not sure if that would apply here, haven’t had chance to look at that yet…

Obviously I don’t know what the original context/query was, but I guess you could extend this fairly easily to pass back the previous scene also, in fact, you could hold on to the last scene and then you could have GetLastScene() as a method, of which the .buildIndex field would be exposed natively. Again, I don’t know what the original context was, so that may be somewhat overkill, and costly depending on the scenario, it would however expose then all of the properties of the scene, so if they are taking that lastSceneIndex() and then going off to retrieve that specific scene to get something relevant from it, that may be an alternative - really depends on the scenario I guess.

Like what you’ve done though, very cool :slight_smile:

1 Like

the original query, was just how to find the index of the previously loaded scene.

Sounds like a plan, need to do a bit of digging tho, I might make some helper methods at some point to make a drag and drop script to get things done a bit easier for other folk and not have their scripts getting cluttered :wink:

1 Like

Dropping it straight into a project would be smoother I think than adding it to an existing script, and perhaps in a tidy namespace, shame this isn’t already part of the UnityEngine.SceneManagement.Utility really, still, you’d not be having all this fun then :slight_smile:

1 Like

definitely, thats one thing i find brilliant on here and discord, is that people have all sorts of wierd and wonderful problems and thoughts, and digging around to find possible solutions and delving deeper than normal is a great way for me to learn about things that I might otherwise overlook :slight_smile:

and popping it all back in here, gives a good cross pollination of info (if thats a thing, cant think of the words, heads blank)
might have to go with a separate script, not sure how to handle the enable and disable hooks, but thats for tomorrow heh

1 Like

I would most definitely agree, helping to resolve issues other people are having is a really useful way to expand on your own knowledge, problems that you may not ever have come across. 'tis a good community for sharing info / asking for help isn’t it… cross-pollination to the max! :slight_smile:

Privacy & Terms