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 /
avatar image
0
Question by JC_Amtote · Jan 18, 2017 at 06:23 PM · editorserializationstaticinstance

Unserialized private variable values from static instance persist when exiting play mode

In our scene, we have a static instance of a 'GameInfo' class that manages unserialized private values such as 'volume' and 'score'. It is observed that while these variables are private, or unserialized, they will retain their values as they get set during editor play sessions instead of resetting to defaults, or '0'. If these values are made public, then they do reset to 0 between play sessions (which is what we want).

For example, the series of events would be something like:

  1. Enter play mode

  2. Acquire a score of 10

  3. Exit play mode

  4. Re-enter play mode

  5. Observe that the score is still '10'

Here is a snippet of our code:

 namespace Assets.Scripts
 {
     /// <summary>
     /// Singleton for high level game information
     /// </summary>
     [Serializable]
     public class GameInfo
     {
         private static GameInfo _instance;
 
         public static GameInfo Instance
         {
             get
             {
                 if(_instance == null) _instance = new GameInfo();
                 return _instance;
             }
         }
 
         private int _volume;
         private int _score;
 
 [...]
      }
 }
Comment
Add comment · Show 1
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 QuentinLG · Jan 18, 2017 at 09:18 PM 1
Share

Your class is marked as serializable. So whether your attributes are private or public they can and will be serialized on demand. So if you have some code somewhere that loads your variables on play (like a save system) then it can change your values. If you want to prevent varaible to be serialized use NonSerializable.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jan 18, 2017 at 06:50 PM

I can not reproduce what you say here. I tried all combinations of public / private and make the class serializable (as you did) / non serializable. In any case whenever i enter playmode the old singleton class instance is destroyed and a new one is created. static variables are reset when you enter playmode but are not reset when you leave playmode.

So if you see something to carry over from edit to playmode it has to be serialized somewhere in the scene or in an asset. Maybe you broke the golden rule of singletons? Never (ever) cache a reference to a singleton. Always use the global accessor, in your case your "Instance" property.

I adden a Debug.Log in the constructor of my singleton class as well as the finalizer (aka destructor) to see when an instance is created and when it is finalized (garbage collected). I also added a button to one of my test editor windows to print the current value of the private variable inside the singleton (accessed through a property).

So for me the singleton instance is destroyed / replaced when i enter playmode. So there's no chance of anything can carry over.

Finally why would you actually put the Serializable attribute on a singleton class? You certainly don't want any other system to create and initialize an instance of that class. Because that instance would not be stored in your instance variable. So if that's really the behaviour you see, there has to be something else you did not mention.

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

6 People are following this question.

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

Related Questions

Object Painter Editor Help 1 Answer

Is EditorUtility.SetDirty restricted to prefabs or inspected GameObject? 5 Answers

EditorWindow: How to Serialize variables after PLay 1 Answer

UnityEvent Serialization Problem 1 Answer

Order of a serializable list not saved correctly using ReorderableList 2 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