ScriptableObject asset not saved to disk

I have this ScriptableObject class:

public class ExampleScriptableObject : ScriptableObject {

        public string testName;
        public int value;
}

which I instantiate with this code:

[MenuItem("Assets/Test/ExampleScriptableObject")]
        public static void CreateExampleScriptableObject() {
            ScriptableObjectUtility.CreateAsset<ExampleScriptableObject>();
 }

and the access it from this MonoBehaviour:

class ExampleMonoBehaviour : MonoBehaviour {

    public ExampleScriptableObject ExampleScriptableObject;
    public string TestName;
    public int Value;

    private void OnValidate() {
        ExampleScriptableObject.testName = TestName;
        ExampleScriptableObject.value = Value;
    }
}

42885-examplemonobehaviour.png

Each time I change value in the inspector and save scene/project, the change is only reflected in the scene file;

but asset file remains empty:

%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
  m_ObjectHideFlags: 0
  m_PrefabParentObject: {fileID: 0}
  m_PrefabInternal: {fileID: 0}
  m_GameObject: {fileID: 0}
  m_Enabled: 1
  m_EditorHideFlags: 0
  m_Script: {fileID: 11500000, guid: 1929e198d1f69204cbed8c2482e19fe9, type: 3}
  m_Name: Test.ExampleScriptableObject
  m_EditorClassIdentifier: 

How can I save changes made to asset to disk instead of to the scene file?

call

EditorUtility.SetDirty(ExampleScriptableObject);

to save your object

Create custom inspector for the MonoBehaviour and in OnInspectorGUI() call EditorUtility.SetDirty() with the ScriptableObject asset passed as argument.

Since this is the top Google result for this issue, I’d like to complement the previous answers by suggesting also taking a look at AssetDatabase.SaveAssets() which effectively saves the changes to disk.

Useful for automated tools and stuff.