Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Scriptable object won't save changes from my editor

Discussion in 'Scripting' started by TheCelt, Sep 13, 2023.

  1. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    721
    My custom editor adds data to a list in my scriptable object but when i save and close unity, reopen the project all the data is lost... why is this?


    In my editor code i cast target to the type and call a public method on the scriptable object like this:


    Code (CSharp):
    1.  
    2. //_data = target as TestObj
    3. _data.Add(new(a,b,c, pos));
    4. EditorUtility.SetDirty(target);
    5.  
    In my scriptable object i have:

    Code (CSharp):
    1.  
    2. //list is serialized as so:
    3. //[SerializeField]
    4. //List<ValueTuple<Vector3, Vector3, Vector3, Vector3>> _data = new(8);
    5. public void Add(ValueTuple<Vector3, Vector3, Vector3, Vector3> values) => _data.Add(values);

    Am i missing something? Why does it not retain the information ?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    What is it a collection of?

    Unity probably isn't going to be able to serialise whatever ValueTuple is.
     
  3. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    721
    It's a built in CSharp type. So we have to save the data in more basic types rather than custom types in order for it to serialize and save it?
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Unity has specific rules as to what it can and can't serialise: https://docs.unity3d.com/Manual/script-Serialization.html

    A struct from a non-Unity library likely hasn't been set up to work with Unity. Usually they aren't decorated with
    [System.Serializable]
    and often use auto-properties that Unity can't serialise anyway.

    You can easily just make a wrapper struct/class that does play nice with Unity for your purpose.
     
  5. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    721
    Ah okay ill just represent my data in a more fundamental types then combine it when needed in runtime.

    Thanks
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Or just do this:
    Just write a struct that wraps these four Vector3s.
     
    TheCelt likes this.
  7. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    Also note if the editor script is a drawer for the given SO type then you should make changes only to the SerializedObject instances of the SO. Any changes made directly to the fields of the target will be lost because the editor works on a temporary instance of the target object, thus all changes need to be serialized back which is what the SerializedObject is for.