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?