Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by TheYokai · Jan 23, 2016 at 12:22 AM · scripting problemapi

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Stralor · Feb 06, 2016 at 02:32 AM

(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)
 SceneManager.CreateScene(newSceneName);
 
 //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!)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by manal1122 · Mar 16, 2019 at 03:10 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

42 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I add music code to my C# without getting a API update prompt? 1 Answer

Steam Leaderboards in Unity, upload time since level load 0 Answers

How to assign an absolute area to a moving gameObject? 1 Answer

How do i make a collage image in Unity? 0 Answers

Just updated to newest Unity version but something's missing 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges