- Home /
Why doesn't my ScriptableObject save using a custom EditorWindow?
Solution
Call EditorUtility.SetDirty(yourObject); on any changes made to it.
I have a problem where a ScriptableObject doesn't save its members properly when edited through a custom EditorWindow. It saves properly when edited through the inspector. I can see through the inspector that the ScriptableObject is affected properly by the EditorWindow.
Am I required to explicitly call some SaveAsset() function on the object when editing it through the EditorWindow? Either that, or it might be some bug in Unity. In the former case, does anyone know how I can save the ScriptableObject? In the latter case, does anyone know of a workaround?
Further, the ScriptableObject contains two ints for width and height, and a one-dimensional array of a custom enum type (basically a flattened 2d array). Both the ScriptableObject and the the custom enum type uses System.Serializable attribute on the class and enum type. All fields are public. No properties are being used. The ScriptableObject also exists in the project, so it is not a transient object.
Currently I am trying to force-save the asset through AssetDatabase.SaveAssets. This approach doesn't seem natural as I don't want to necessarily save all assets, only the one I am editing.
However, as I found out after some trial and error AssetDatabase.SaveAssets doesn't actually save the asset as expected.
Answer by Lucas Meijer 1 · Feb 15, 2010 at 11:33 AM
You need to call EditorUtility.SetDirty() on your scriptable object.
That did the trick! (Apparently ScriptableObject.SetDirty also works although it is obsolete.) EditorUtility.SetDirty it is!
This works great. It's worth noting that if you're calling this from an EditorWindow on the Selection.gameObjects array for example, you need to pass the component you modified to SetDirty() and not the gameObject.
This doesn't work for two-dimensional array. Doesn't anyone know how to fix that? I am using Unity 5.
This certainly did do the trick, I was calling AssetDatabase.SaveAssets(); in the if (GUI.changed) {} which is what the tutorials from unity had me believe (not GUI.changed but the save)... right next to the other Ondirty call... I guess this is specific to the editor window. Anyway wasted a couple of days reworking my data structures as some were showing up as Type $$anonymous$$ismatch, again this probably wasn't helping... but the stars have aligned :) thanks Lucas.
Answer by bodowens · Nov 01, 2017 at 08:13 AM
I tried EditorUtility.SetDirty(...)
, Undo.RecordObject(...)
, SerializedObject.Update()
, SerializedObject.ApplyModifiedProperties()
, none of them have any effect. The only thing that did have effect is AssetDatabase.SaveAssets()
, but that causes horrible stutter.
I thouth that maybe Unity only saves modified assets on exit, so I tested that too. It didn't work.
Have you found a solution? I seem to be having the same problem. I have tried EditorUtility.SetDirty(), AssetDatabase.SaveAssets(), Undo.RecordObject(), and none seem to be working.
@dustin_pau1 Have you found a solution ? I'm getting the same problem...
For me it finally worked when I used EditorUtility.SetDirty(...) without using AssetDatabase.SaveAssets()
Answer by ecesis_llc · Feb 16, 2016 at 12:32 AM
I know this is answered, but I came across this topic while trying to resolve a similar issue. If you are making edits to non-unity objects, like custom structs, but the change is not reflected you may want to use Repaint().
Your answer
Follow this Question
Related Questions
[Solved]Why doesn't my ScriptableObject based asset save using a custom inspector ? 1 Answer
How should I serialize data that is also editable in the Inspector? 2 Answers
How to make a serialized editor only field 2 Answers
pass child class to ScriptableObject.CreateInstance<> ? 1 Answer
Serializing list of base and derived classes with custom inspector 1 Answer