Why does value remain after deleting and restoring [SerializeField] attribute?

I’ve noticed interesting behaviour with serialized variable

using UnityEngine;

public class Test : MonoBehaviour
{
    [SerializeField] int a;
}

What I do:

  1. Create script with serialized variable
  2. Assign non-zero value in the Inspector
  3. Delete attribute, save, wait for scripts to recompile
  4. Add it back, save, wait for scripts to recompile
  5. Value in the inspector is somehow restored to the one that I assigned earlier

Why does it happen?

1 Like

The old value stays in the file on disk until you re-save your scene/prefab/scriptable object/etc. (I think Unity’s serialization system may also be able to keep those old values around internally until you restart the editor, but I’m not sure.)

When you start using source control you will see immediately that the addition of a public / serialized field produces a corresponding entry in the YAML of prefabs / scenes that contain that script.

This will only occur when you actually save each scene or prefab.

Same goes for removing such fields. Nothing about Unity would ever decide to go ransack your entire project (which could have millions of uses of that script) just to delete some stray data.

This also has data security implications. If you do a GameJam where you have a public string myPassword; field and for expediency you fill it out with JOSHUA, then realize "oh no, better make that field private and get it out of the keystore, it is on you to go make sure your seekrit data doesn’t still exist in prefabs / scenes.

Screenshot 2024-07-23 at 10.59.21 AM

1 Like