- Home /
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?
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.
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?
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:
call the constructor like it does now
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)
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 ;)
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();
}
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.
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
![](https://koobas.hobune.stream/wayback/20220612080837im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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