Possible Unity Serialization Bug: Retaining Old Default Values in MonoBehaviour

In a MonoBehaviour script, if I create a variable with a default value (A) and save the script, then immediately change its default value to (B), I expect the component's serialized value to be the latest default value (B).

However, the actual result is that it retains the previous default value (A). Why does this happen? The prefab or scene data should not have any information about the variable because I never opened or saved it. In fact, when I checked the file directly, there was no information regarding the variable.

So, how can it still load the previous default value (A)? Is it Unity serialization bug?

I'm using Unity 2021.3.23 and Rider. Thanks.

Your expectation is wrong. If it did this, anything that was persisted would all be reset and that would be disasterous for your project.

If you want to reset the persisted value then you have to reset the Script component. See the option in the inspector next to the component.

Not a bug.

https://docs.unity3d.com/Manual/UsingComponents.html

1 Like

Your sentence is already a contradiction in itself. You do understand what “serialized” means, right? It means saving the value on disk. So yes, once the value is serialized, you will use that serialized value from now on. That’s the point of having a serialized value. So your expectation is already wrong. The default value is ONLY used when the object is first created when nothing has been serialized yet.

No, It didn’t serialized. I checked prefab data, and there’s nothing.

Then, where does Unity obtain the previous default value ‘(A)’ from? The value wasn’t serialized and doesn’t exist in the prefab data. Where does it come from?

What do you mean by [quote=“KangminLee424242, post:4, topic: 917470, username:KangminLee424242”]
I checked prefab data
[/quote]?
Where is the value serialized? A prefab asset or a prefab instance in the scene? Also there are different kind of prefabs and some modifications may be stored in the meta file. Also did you close the editor before you “checked the prefab data”? Sometimes things may not be written out to disk yet.

Yes, I’m referring to a prefab asset. I didn’t modify it, and I closed and reopened the editor. The variable wasn’t serialized; I checked this just a moment ago.

If I change the variable in the Inspector and save it, the change is reflected in the prefab data file. I checked this as well.

Which of these? So do we talk about a normal (Regular) gameobject prefab that does not come from an imported model but was created in the scene and then turned into a prefab? So the prefab actually has the file extension .prefab? Also you talk about the value of the prefab itself, not an instance of that prefab that lives in the scene? Serialized data of prefab instances are stored in the scene.

Whenever Unity does a domain reload, it reserialises all assets, adding in any additional fields that weren't present already with default values. Once that's serialised in, changing the default value won't change the value serialised into existing assets at that point.

So you added a field with default value A, reloaded Unity, Unity reserialised all assets and added the field with default value A. Naturally, changing the default value to B won't change anything in existing assets.

You most likely just didn't see the initial serialised value in the asset's yaml. Unity's always worked this way.

1 Like