I am working on creating a tool to take our JSON item DB from a custom tool and convert them into Scriptable Objects. I’ve created an editor script that currently correctly creates a scriptable object.
var so = ScriptableObject.CreateInstance(itemClass);
//assign values here
AssetDatabase.CreateAsset(so, "Assets/Items/" + so.name + ".asset");
Hypothetically this contains a color for the item, and I assign it to a script that updates the material color to blue. Later our json says the item should be red. Running the script again causes the SO to update correctly BUT breaks the reference to the item in the scene(which weirdly doesn’t error but instead just stays as the old copy) I need the item to be updated instead of creating over the existing one.
Currently I have done some googling which says I have to get the asset and then update it, set it to dirty and then save which is not working:
var existingSO = AssetDatabase.LoadAssetAtPath(
"Assets/Items/" + so.name + ".asset", typeof(ScriptableObject)
);
existingSO = so;
EditorUtility.SetDirty(existingSO);
AssetDatabase.SaveAssets();
I can’t see any kind of “UpdateAsset” function for scriptable objects anywhere so I’m not sure how else to update the values.