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 UsmanAbbasi · May 15, 2016 at 12:57 PM · c#serialization

Serializabel class not updating

I have a Serializable class and I make a public object of this class in another class like shown in the code:

 [System.Serializable]
 public class PlayerStats
 {
     public float health;
     public float energy;
     public float _XP;
 
     public float maxHealth = 100;
     public float maxEnergy = 100;
 
     public PlayerStats()
     {
         energy = maxEnergy;
         health = maxHealth;
         _XP = 0;
         Debug.Log ("ctor");
     }
 }
 
 
 public class PlayerStatsManager : MonoBehaviour {
 
 
     public PlayerStats _playerStats;
 }

When I change the value of any of the variables in "PlayerStats" class, it does not get reflected /changed in inspector. But when I remove the "PlayerStatsManager" from the gameobject and then add it again, then values get updated. Example: if I change "maxEnergy" to 200, it will not get changed unless I remove and add the script again on gameobject. Can someone explain why this is happening. Does this have anything to do with Serialization?

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
1

Answer by troien · May 15, 2016 at 01:12 PM

How/ when are you changing any of the variables?

Through an editor script? in edit mode, in playmode?

Or do you mean when you change this line of code

 public float maxHealth = 100;

To something like this for instance:

 public float maxHealth = 50;

If so, that is intended behaviour. (Or at least expected). This is because the values are serialized when you add the component. When you then change the default values of the script. You don't change the serialized values. And obviously, when you change the serialized values in the inspector you don't change the default values in the code. They are 2 different values. Which in a way makes sence, if you set up multiple objects where you set different values to each variable, you don't want them to all change whenever you change the default script value.

Same applies when maxHealth would be located directly into PlayerStatsManager rather then PlayerStats btw.

I believe clicking the cog icon (to the right of the script name in the inspector) and hitting reset can also be used to reset your serialized values back to your default ones.

Comment
Add comment · Show 2 · 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 UsmanAbbasi · May 15, 2016 at 01:19 PM 0
Share

I am changing the code line like this:

 public float maxHealth = 50;


I understand what you are saying, but what is confusing me is that I have a constructor which assigns the value to my variables. Constructor is running every time I change something then why the assignment statements are not assigning new values in ctor?

avatar image troien UsmanAbbasi · May 16, 2016 at 09:59 AM 1
Share

Because they are basically overwritten by the serialized values after the constructor is called. If you would call the constructor manually from the Awake method of PlayerStats$$anonymous$$anager (by saying _playerStats = new PlayerStats()) , then it would:

  1. call the constructor like it does now

  2. overwrite any value that is serialized like it does now (Note that when adding the script, Unity doesn't have any serialized value yet, and thus won't overwrite anything)

  3. Execute your awake method and overwite everything again :p.

Also note that unlike things that derive from UnityEngine.Object, any of your custom classes that have the [Serializable] attribute are stored as value ins$$anonymous$$d of a reference. So if 2 monobehaviours have a reference to the same Serialized object, after serializing/deserializing they would reference 2 unique objects with the same values.

See also the manual ;)

avatar image
1

Answer by Bunny83 · May 15, 2016 at 01:21 PM

You have a wrong picture of how serialization works. Things you set in the constructor are irrelevant once the class got serialized. Whenever the class is deserialized it is first recreated (your constructor is called) and then the values are set to the values that has been serialized.

You might want to add a NonSerialized attribute to the variables you don't want to be saved. Furthermore you might want to use a seperate method to actually reset your stats which you call after the deserialization has been completed. So either in Awake or Start of PlayerStatsManager.

  [System.Serializable]
  public class PlayerStats
  {
      public float health;
      public float energy;
      public float _XP;
  
      public float maxHealth = 100;
      public float maxEnergy = 100;
  
     void Reset()
     {
         energy = maxEnergy;
         health = maxHealth;
         _XP = 0;
     }
 }


and in Awake:

 void Awake()
 {
     _playerStats.Reset();
 }

Comment
Add comment · Show 2 · 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 UsmanAbbasi · May 15, 2016 at 01:25 PM 0
Share

Can you explain a little more about serialization? i have read serialization from unity website and I have basic idea how it works but this case is not clear to me.

avatar image 00jknight · Oct 26, 2017 at 05:18 PM 0
Share

Heads up that Reset is a built in $$anonymous$$onoBehaviour function, so while it will work here in PlayerStates, people should know not to blindly use it in $$anonymous$$onoBehaviours.

https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.Reset.html

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

146 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 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 avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

In search of a component that acts as a dynamically typed, serializable list container 0 Answers

Nested Dictionary data lost after playmode 0 Answers

Object says it's null, but on inspecting it seems to have the correct value 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