- Home /
Serialization issue with ScriptableObject
Hello, I've been having a serialization issue using scriptableobjects and nested lists of serializable objects. This is nothing new but the previous question hasn't really helped me.
Basically, I have a wrapper scriptable object with the following attributes in it:
public class PropChunkContainer : ScriptableObject
{
public PropChunkData data;
public string chunkName;
public float weight;
}
Where PropChunkData is a class marked as [Serializable] and it contains a bunch of primitives, mostly floats and some strings. More notably it contains a list of itself like the following:
[Serializable]
public class PropChunkData
{
public string propName { get; set;}
public float positionX { get; set; }
public float positionY { get; set; }
public float positionZ { get; set; }
...
public List<PropChunkData> children;
}
I then have an editor script which is supposed to save/load these items, and the error occurs after a restart which is why I believe it's a problem with serialization. If I save and load, it works exactly as it should and all the PropChunkData objects have their correct values. However if I save and restart the Unity editor, then these values are lost. So upon trying to load, the chunkName and weight from PropChunkContainer will deserialize correctly and I can read them, but all the PropChunkData objects will be filled by zeroes and empty strings.
The following is the code I have been using to save objects:
PropChunkContainer dataContainer = ScriptableObject.CreateInstance<PropChunkContainer>();
dataContainer.Initialize(data, chunkName, weight);
AssetDatabase.CreateAsset(dataContainer, propChunkDirectory + chunkName + ".asset");
EditorUtility.SetDirty(dataContainer);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
And this code to load (the weird code because LoadAssetAtPath did not work for some reason, so I loaded all and looped over all of them instead):
Object[] assets = AssetDatabase.LoadAllAssetsAtPath(targetPath);
for (int i = 0; i < assets.Length; i++)
{
if (assets[i].GetType() == typeof(PropChunkContainer))
{
PropChunkContainer container = (PropChunkContainer)assets[i];
if (container != null)
{
LoadContainerIntoScene(container);
}
break;
}
}
Thanks for reading, I sure hope someone can help me with this.