Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Serialized Fields occasionally losing value

Discussion in 'Scripting' started by Ziplock9000, Oct 8, 2019.

  1. Ziplock9000

    Ziplock9000

    Joined:
    Jan 26, 2016
    Posts:
    360
    I have this rare occurrence with objects in my scene that have a string serialised field that loses it's value. This occurs at fairly rare random times but slightly more often when I upgrade the version of Unity I'm using, but not always. I can go weeks without it happening and then all of a sudden it happens to several objects all at the same time out of several 100 in a scene that are fine.
    This is a huge issue as I use this field to give certain objects in a scene a GUID/UUID (as a string) which must never change once the object is placed.
    I know the string is becoming null because I have code in the OnValidate event that generates a new GUID when the field it blank or null which should only occur once. But occasionally it does generate a new one at random times because the check of the field comes up as null. It's happened with the last dozen of so versions of 2017.x 2018.x 2019.3.x
    It's inconsistent random occurrence is what baffles me the most. I wish I could debug the scene files when this happens and do a DIFF as that's where the serialised field data is stored when the object is placed into the scene and the GUID generated. But with them being binary I cant
    Any help appreciated.
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    You can tell Unity to serialize assets (including scenes) as text, but I have found that diffing scenes is still awful because Unity will constantly re-serialize floating-point values as slightly different numbers for no apparent reason (e.g. changing 1 to something like 1.00000004), leading to a large number of spurious differences between saves.
     
  3. Ziplock9000

    Ziplock9000

    Joined:
    Jan 26, 2016
    Posts:
    360
    I've had this as "Force Text" for years now and some of the larges scene files have stayed as binary while the smaller ones of text. I've tried toggling the option and saving to no avail.

    UPDATE: After testing it seems that because the terrain has a scene, it ignores the "Force Text" option and saves as binary anyway.. Understandable, but not obvious.
    Anyway this is not my immediate issue.
     
    Last edited: Oct 8, 2019