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

Data not serializing/saving

Discussion in 'Scripting' started by Flavelius, Aug 17, 2017.

  1. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    943
    Hi,
    i have a script with a custom inspector; in this inspector i use a button to gather some data (multiple instances of a very simple custom [serializable]-attributed class) and store that in a public list of the inspected component.
    I'm not using serializedObject with the serializedproperty GUI/drawers in the custom inspector but the plain casted reference to the component to access its fields ( (mycomponent)target ).
    I even added a SetDirty call, but the data won't persist.
    The GameObject resides in the hierarchy, it's not a prefab.
    The serialized data survives playmode but not editor restart and that irritates me.
    Is there anything i missed?
     
  2. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    943
    i'm expecting this data to be saved just as i do when changing other basic variables on Gameobjects in the scene (they are serialized into the scene stream, no?)

    Edit: retrying several times, at some point it now randomly did retain the values between restarts but then new ones are still not replacing those (after restart the previous values are restored).
     
    Last edited: Aug 18, 2017
  3. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    943
    After further tests: new values are only saved while i create a prefab of the gameobject. I can even delete the prefab directly after creating it, so it doesn't look like the object just gets those values from the prefab.
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    Since Unity 5.3, SetDirty doesn't mark scene objects as dirty. Using the serialized object representation is the preferred way to go. But if you really don't want to do that, use Undo.RecordObject() before making changes.
     
    Flavelius likes this.