C# Unity5.3 SceneManager -- Unloading and Loading a specific instance of a Scene.

This has been bothering me for a while now since I’ve been trying to update to 5.3. Let me run over a few issues I’ve been having with the new SceneManager class.

The first is that LoadScene doesn’t return the specific instance of a scene that I’ve loaded. If I load two instances of the same scene, let’s just call that scene ‘mainmenu’, the two instances are considered to be different in unity’s point of view, which makes sense. Additively loading the same scene twice should result in slightly diverged scenes, and thus should be considered to be different. However, the SceneManager API doesn’t provide a good way of dealing with these divergent scenes of the same name. Ideally, I should be able to say “Hey SceneManager, Load the ‘main menu’ scene” and it should give any data it can about said scene so that I can be assured that I have access to the specific instance that I want loaded. Instead, with the current API, all I can say is “Hey SceneManager, I need a scene by the name of ‘mainmenu’” which returns the first scene by that name in the SceneManager’s internal list of scenes, which means that I have no clue which instance of the Scene I’m dealing with at any given point.

However, there are a few ways you can work around this limitation. Specifically, you can manually iterate through the list of scenes that Unity’s SceneManager has stored and cross check them with scenes you have previously loaded. This works, but is hacky, and it eventually leads into the second problem –

Which is that, even knowing that I have a specific instance of a scene, I unfortunately have no ability to tell Unity to unload a specific instance of a scene. Because the SceneManager API only has a Unload function that accepts either a string or build index number, there’s no way for me to tell Unity3d to unload a specific instance of a Scene I may have loaded more than once. Let’s say that I have two instances of a scene called ‘splashscreen’ loaded at once and I have two Scene objects that reference to the two different, divergent instances of SplashScreen – how exactly am I supposed to be able to unload a specific instance of said SplashScreen when there’s method that lets me just pass the Scene struct as an argument?

I’m really hoping there’s some sort of thing that I’m missing – which is why I’m here now. Additively loading the same scene more than once isn’t really a critical feature, and I could just program it so that there’s no tangible way to let the user do this, but since Unity3d allows for multiple instances of the same scene to be loaded, it would also be nice to know if there’s a proper way to deal with this. Funnily, if SceneManager.LoadScene returned a Scene and if SceneManager.UnloadScene took a Scene, then all of the major problems I have would be solved. Perhaps this is a design oversight as SceneManager.UnloadScene’s documentation actually lists that Scene is a potential argument, but there’s no function listed above that has it as an argument nor is there any that my IDE can find. I even tried just typing it in to see if it would work out of desperation one time (and curiosity), but that of course did nothing.

Has anyone found a good way of dealing with having multiple instances of the same scene?

(Note: untested, purely theoretical, probably works like a charm)

For Loading, rename all scenes as you load them. Something like this:

//Load the scene you want (Note: sceneToLoad is a string in this example)
SceneManager.LoadScene(sceneToLoad, LoadSceneMode.Additive);

//Get that scene (should be the only instance if you do this every time)
Scene sceneJustLoaded = SceneManager.GetSceneByName(sceneToLoad);

//Then, make a new scene name
//Set this to something unique each time so it works with any number of scenes. Lazy example:
string newSceneName = sceneToLoad + countOfScenesLoadedSoFar.ToString();

//Create an empty scene with the unique name (this is new and yet undocumented, might be 5.3.2)

//Get that scene
Scene emptyScene = SceneManager.GetSceneByName(newSceneName);
//Merge our loaded instance with our empty
SceneManager.MergeScenes(sceneJustLoaded, emptyScene);

Bam, you’ve got a unique code to that scene you want to reference/ unload later.

(You’ll have to store the name somewhere, of course!)

i am having the 2018 version and “SceneManager.LoadScene();” code is not working it shows me error what should i do???