- Home /
Saving a ScriptableObject with complex fields
have a class that inherits from ScriptableObject with a few fields that can't be serialized and one that can be. In another script I create an instance of the ScriptableObject, set all of its fields and then save it using UnityEditor.AssetDatabase.CreateAsset(string path). I have a final script with a serialized field of the ScriptableObject.
When adding the newly saved ScriptableObject to the third script and attempting to read data from it, the List and the Dictionary are both set to null, while the Sprite retains the value it was set to.
Is there any way to save the List and Dictionary to the .asset file?
My code is as follows:
The ScriptableObject:
public class MyScriptableObject : ScriptableObject
{
public Dictionary<string, Sprite> sprites;
public Sprite sprite;
}
Where I save the ScriptableObject through code:
MyScriptableObject myObject = (MyscriptableObject)ScriptableObject.CreateInstance("MyScriptableObject");
Dictionary<string, Sprite> sprites = new Dictionary<string, Sprite>();
//loadedSprites is a [SerializeField] List<Sprite> present in this class
for(int i = 0; i < loadedSprites.Cout; i++)
{
sprites[i + ""] = loadedSprites[i];
}
myScriptableObject.sprites = sprites;
myScriptableObject.sprite = loadedSprites[0];
Where I attempt to read the saved ScriptableObject elsewhere:
//loadedMyScriptableObject is a [SerializeField] MyScriptableObject present in this class
//and I drag the saved asset onto this script
//when this code is run, loadedMyScriptableObject.sprites is null,
//but loadedMyScriptableObject.sprite is what it was set to in the previous code
List<Sprites> sprites = loadedMyScriptableObject.sprites;
Sprite sprite = loadedMyScriptableObject.sprite;
Your answer
Follow this Question
Related Questions
Prevent changes in ScriptableObject type Asset in Editor. Dont save it. 0 Answers
Can I safely put a prefab asset into version control with Unity Free? 3 Answers
Save procedural mesh in the editor as asset. 2 Answers
AssetDatabase.ExportPackage not working 3 Answers
Ho do I find all scenes with a certain script on an GameObject 2 Answers