How do you serialize Inherited ScriptableObject variables?
- Data.cs -
[System.Serializable]
public class Data : ScriptableObject
{
public string path = null;
}
- ChildData.cs -
[System.Serializable]
[CreateAssetMenu( fileName = "ChildData", menuName = "Data/ChildData", order = 200 )]
public class ChildData : Data
{
public string secondString;
}
Now when I make a ScriptableObject asset of ChildData it shows both path and second string fields; however when I build "path" reverts to null as if it were not serialized.
Everything says that ScriptableObjects support inheritance serialization, so what am I doing wrong here?
I have multiple classes akin to "ChildData" that use that path, none of them serialize it.
Is my only recourse to create virtual read/write methods in Data and override them in every child class to store to a string variable local to that child data serialized object?
Answer by NTG-Sera · Sep 20, 2019 at 03:04 PM
Well a full day later and it finally posts the question; regardless I've figured out the issue.
In my workflow I had no problems assigning to ChildData.path = "Example/Path" through editor code, seeing those changes persist through runs and builds, only to re-check those values when running the second instance to find them null.
I eventually tracked the issue down, and I'm speculating at the underlying behaviour, but it appears that ScriptableObject state is asynchrnous to the editor state and will refresh from file whenever it pleases and not on a consistent basis.
Because I was not doing this:
SerializedObject so = new SerializedObject( data );
so.Update();
so.FindProperty( "_path" ).stringValue = _path;
so.ApplyModifiedProperties();
The changes were never committed to file, not even on a full project save, even if they appeared to be committed to the ScriptableObject state.
It appears that inherited serialization works just fine (for clarification the base classes are in their own individual respectively named files) and it was just an issue with how unity saved those changes.
Your answer
![](https://koobas.hobune.stream/wayback/20220612212405im_/https://answers.unity.com/themes/thub/images/avi.jpg)