Search Unity

Bug Possible Unity Serialization Bug: Retaining Old Default Values in MonoBehaviour

Discussion in 'Scripting' started by KangminLee424242, May 10, 2023.

  1. KangminLee424242

    KangminLee424242

    Joined:
    Mar 20, 2023
    Posts:
    4
    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.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    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
     
    Bunny83 likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,011
    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.
     
  4. KangminLee424242

    KangminLee424242

    Joined:
    Mar 20, 2023
    Posts:
    4
    No, It didn't serialized. I checked prefab data, and there's nothing.
     
  5. KangminLee424242

    KangminLee424242

    Joined:
    Mar 20, 2023
    Posts:
    4
    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?
     
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,011
    What do you mean by
    ?
    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.
     
  7. KangminLee424242

    KangminLee424242

    Joined:
    Mar 20, 2023
    Posts:
    4
    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.
     
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,011
    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.
     
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,938
    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.
     
    Bunny83 likes this.