ScriptableObject wont save changes.

Hello everyone,

I’m having some trouble working with ScriptableObjects.
I have a ScriptableObject that looks like this:

    public class DataHolderObj : ScriptableObject
    {
        public string Name = string.Empty;
        public Sprite Background;
        public Color BackgroundColorTint = Color.white;
        public List<Symbol> Symbols;
    }

My Symbol class looks like this:

    [System.Serializable]
    public class Symbol
    {
        public Sprite SymbolSprite;
        public string SymbolName = string.Empty;
    }

Now, instead of editing it in the inspector, I made a wizard like window (the object is more complex then I’ve shown).

My window class looks like this:

    public class DataHolderObjWindow : EditorWindow
    {
        private DataHolderObj _dataHolder;

        public void Init(DataHolderObj holder, bool isNew)
        {
            _isNew = isNew;
            if (isNew)
            {
                holder.Init();
            }
            _dataHolder= holder;
        }
// some OnGUI stuff
  
if (_isNew)
            {
                if (GUILayout.Button("Save"))
                {
                    _machineData.GenerateVirtualReels();

                    var path = DefaultPath + _dataHolder.MachineName + ".asset";
                    AssetDatabase.CreateAsset(_dataHolder, path);
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();
                    EditorUtility.FocusProjectWindow();
                    Selection.activeObject = _dataHolder;
                }
            }
            else
            {
                if (GUILayout.Button("Save Changes"))
                {
                    EditorApplication.SaveAssets();
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();
                }
            }
}

And the code that opens the window:

        [MenuItem("Scriptable Objects/Create DataHolderObj")]
        static void DataHolderObjScriptableObject()
        {
            // Get existing open window or if none, make a new one:
            var window = (DataHolderObjWindow)EditorWindow.GetWindow(typeof(DataHolderObjWindow), true, "Object Editor");
            window.Show();

            var asset = CreateInstance<DataHolderObj>();
            window.Init(asset, true);
        }

        [MenuItem("Assets/Edit DataObj")]
        private static void EditMachine()
        {
            // Get existing open window or if none, make a new one:
            var window = (DataHolderObjWindow)EditorWindow.GetWindow(typeof(DataHolderObjWindow), true, "Object Editor");
            window.Show();

            window.Init((DataHolderObj)Selection.activeObject, false);
        }

        [MenuItem("Assets/Edit DataObj", true)]
        private static bool EditMachineValidate()
        {
            return Selection.activeObject.GetType() == typeof(DataHolderObj);
        }

Now for my problem:
If I open the window from the top menu (creating a new data) everything works as intended and the ScriptableObject is saved.

If I edit the ScriptableObject from the inspector, all changes are saved correctly.
If I edit the ScriptableObject by right clicking on it (Edit DataObj) the window loads the correct data and i can modify it and after closing the window I can see the modified data in the inspector.
But, if I close Unity and open again, the modified data is gone.

Any ideas?

Apparently just adding

EditorUtility.SetDirty(_dataHolder);

fixes the issue :stuck_out_tongue:

1 Like

You are right :slight_smile: the following documentation page also mentions this: http://docs.unity3d.com/ScriptReference/EditorUtility.SetDirty.html

Specifically: