Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by hero14 · Jun 07, 2012 at 02:48 AM · scenes

Keeping Scene States

Hi, i have a scene (scene one) that links to other (scene two) by clicking an object. The scene two has a little minigame, and either you win or lose, links you to the scene one, the thing is that when you´re redirected to the scene one, the previous game advance it´s lost, like the score and the player´s position. I just want to keep that information when changing between scenes, and i´ve search and mentions the use of the DontDestroyOnLoad() but seems not to work, also the use of singletons, wich i don´t understand, so please any solutions..??

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
0

Answer by Berenger · Jun 07, 2012 at 02:56 AM

DontDestroyOnLoad() tells a gameObject not to destroy itself when a new scene is loaded. You could use that on a gameObject to which a script containing the score is attached.

You should use a static var. They are accessed through the class, not through an instance of the class, and remains as long as the app is running.


YOU DON'T NEED A SINGLETON, THE FOLLOWING IS JUST FYI

A singleton is a design, a common technique which is define by : There must be only one instance of it, and You must be able to access it from everywhere. (Not an official definition, that's my how-I-understand-it definition). Basically, the trick is to have a public static var that refers the instance of the class, or create it if there is none yet. Like this :

 public class MySingleton
 {
     private static m_Instance = null;
     public static MySingleton instance
     {
         get{
             if( m_Instance = null ) m_Instance = new MySingleton();
             return m_Instance;
         }
     }
 }
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 hero14 · Jun 07, 2012 at 03:45 AM 0
Share

The thing is that if i attach a DontDestroyOnLoad to a gameobject, when my other scenes is loaded, the objects with that method "overlaps" with the recent loaded scene, i used a static var with the score, and it works, but just with the variables, the GUI components and GameObjects loads from the beginning, i want to load them just as the state they was..

avatar image
0

Answer by Kinetic · Jun 07, 2012 at 07:04 AM

You can load the minigame with LoadLevelAdditive so it loads on top of the previous scene, that way your main game state will remain.

Besides static vars there is another way to preserve data between scenes, you can modify a prefab directly without instanciating it, this prefab will keep that data between scene changes.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

switching scenes 1 Answer

Load specific scene in editor 2 Answers

Is it possible to have multiple scenes saved in memory? 1 Answer

Scene changing and multiple spawn points 1 Answer

Saving current scene in another scene during gameplay? 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