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
0
Question by sk8terboy4 · Jun 23, 2016 at 12:28 AM · sceneunity5scene-loadingscene-switchingscene change

Loading and Unloading scenes

I have one scene that I will like to unload (Scene A) when I click a button. I do not want the scene to be deleted because I want to save the data. I basically want the scene to disable. Once it's disable, I have another scene that will load addictive (Scene B) and will be the active scene in the game. I then want to reverse this by removing Scene B and enable Scene A then set Scene A as the active scene. I tried to do this with SceneManager, but I had no success. It will load Scene B, but it will not disable/unload Scene A?

 public void loadHelpScene(string id)
     {
         SceneManager.LoadScene(id, LoadSceneMode.Additive);
         if (SceneManager.UnloadScene(sceneName))
             print("Success");
 
     }

SceneName is the current scene loaded and id is the scene to be loaded additive.

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 VindictPL · Feb 15, 2017 at 02:49 PM

You can load scene B additive, set it as active scene and disable all root GameObjects in scene A. When you want to get back to scene A just enable them, set scene A as active and unload scene B

To disabe:

 foreach(GameObject g in SceneManager.GetActiveScene().GetRootGameObjects()){
                                     g.SetActive (false);
                                 }

To enable:

 foreach(GameObject g in SceneManager.GetSceneByName("A").GetRootGameObjects()){
                                         g.SetActive (true);
                                     }
Comment
Add comment · Show 1 · 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 Siblyx · Aug 06, 2018 at 10:31 AM 0
Share

Hi, i have the same problem as OP, This code works disabling and enabling the scenes, but why does it double the next loaded scene?

avatar image
0

Answer by LibertyCoding · Aug 23, 2018 at 05:04 PM

You're doing LoadScene as additive. Which will load another. You will need to Iterate over the loaded scenes or by name and check if it is already loaded and inactive and then enable all root objects on that scene instead of loading it again.

Something like this:

 private void SetGameObjectsActive(GameObject[] objects, bool active)
 {
     for (int i = 0; i < objects.Length; i++)
     {
         objects[i].gameObject.SetActive(active);
     }
 }
 
 public void loadHelpScne(string id)
 {
     bool foundScene = false;
             
     for (int sceneIndex = 0; sceneIndex < SceneManager.sceneCount; sceneIndex++)
     {
         var scene = SceneManager.GetSceneAt(sceneIndex);
         if (!scene.IsValid()) continue;
         if (scene.name.Equals(id))
         {
             SetGameObjectsActive(scene.GetRootGameObjects(), false);
         }
         else if (scene.name.Equals(sceneName))
         {
             SetGameObjectsActive(scene.GetRootGameObjects(), true);
             foundScene = true;
         }
     }
     if (!foundScene)
     {
         SceneManager.LoadScene(id, LoadSceneMode.Additive);
     }
     sceneName = id;
 }

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

71 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 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

Keeping track of the previous scene 1 Answer

Save and load scene from file 2 Answers

Event when the scene is loaded 1 Answer

Script is detecting input with ZERO actions 0 Answers

Switching active scene to loaded additive scene does not change camera/render 1 Answer


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