ScriptableObject with Custom Editor resetting data in inspector

I have created a scriptable object and for that scriptable object i have also created a custom editor. Now my issue is that, any changes that i apply to the scriptable object in the inspector, if I leave it and come back, all of the data is reset or gone. How can I make sure that data is saved to my target?

ScriptableObject class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
[CreateAssetMenu(menuName = “Conversation”)]
public class ConversationAsset : ScriptableObject
{

[System.Serializable]
public struct Dialogues 
{
    [SerializeField]
    public Person speakingPerson;

    [SerializeField]
    public string currentMessage;

    [SerializeField]
    public bool containsSpeechRecogniser;

    [SerializeField]
    public bool isQuestion;

    [SerializeField]
    [HideInInspector]
    public Question question;
}

[SerializeField]
[HideInInspector]
public List<Dialogues> dialogues = new List<Dialogues>();

}

CustomEditor:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(ConversationAsset))]
public class ConversationEditor : Editor
{
/Data not saving/
ConversationAsset conversationScript;
SerializedObject GetTarget;
SerializedProperty listOfDialogues;
public int listSize = 0;
private void OnEnable()
{
conversationScript = (ConversationAsset)target;
GetTarget = new SerializedObject(conversationScript);
listOfDialogues = GetTarget.FindProperty(“dialogues”);

    conversationScript.dialogues = new List<ConversationAsset.Dialogues>();
}
public override void OnInspectorGUI()
{
    base.OnInspectorGUI();

    EditorGUILayout.LabelField("Number of Dialogues (Max is 20)");
    listSize = EditorGUILayout.IntSlider("Number", listSize, 0, 20);
    try
    {
        if (Event.current.type == EventType.Layout)
        {
            if (listSize < listOfDialogues.arraySize)
            {
                conversationScript.dialogues.RemoveAt(listOfDialogues.arraySize - 1);
            }
            if (listSize > listOfDialogues.arraySize)
            {
                ConversationAsset.Dialogues newDialogue = new ConversationAsset.Dialogues();
                conversationScript.dialogues.Add(newDialogue);
            }
        }
    }
    catch (System.ArgumentOutOfRangeException ex)
    {
        Debug.Log("Argument out of range " + ex + " resetting list size.");
        listSize = 0;
    }

    

    GetTarget.UpdateIfRequiredOrScript();

    for (int iteration = 0; iteration < listOfDialogues.arraySize; iteration++)
    {

        ConversationAsset.Dialogues thisDialogue = conversationScript.dialogues[iteration];
        //ConversationAsset.Dialogues thisDialogue = listOfDialogues.GetArrayElementAtIndex(iteration);
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("Dialogue " + (iteration +1));

        thisDialogue.speakingPerson = EditorGUILayout.ObjectField("Person Speaking", thisDialogue.speakingPerson, typeof(Person), true) as Person;
        thisDialogue.currentMessage = EditorGUILayout.TextField("Speaking Message", thisDialogue.currentMessage);
        thisDialogue.containsSpeechRecogniser = EditorGUILayout.Toggle("Contains a Speech Recognizer", conversationScript.dialogues[iteration].containsSpeechRecogniser);
        thisDialogue.isQuestion = EditorGUILayout.Toggle("Is Question", conversationScript.dialogues[iteration].isQuestion);

        if (thisDialogue.containsSpeechRecogniser)
        {
            thisDialogue.isQuestion = false;
        }

        if (thisDialogue.isQuestion)
        {
            var possibleAnswers = thisDialogue.question.possibleAnswers;
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Question for Element " + (iteration+1));
            thisDialogue.question.questionType = (Question.TypeOfQuestion)EditorGUILayout.EnumPopup("Question Type", thisDialogue.question.questionType);
            EditorGUILayout.LabelField("Number of Possible Answers (Keep it between 0 and 10)");
            int countOfAnswers = EditorGUILayout.IntField("Number", possibleAnswers.Count);
            while (countOfAnswers < possibleAnswers.Count)
            {
                possibleAnswers.RemoveAt(possibleAnswers.Count - 1);
            }
            while (countOfAnswers > possibleAnswers.Count)
            {
                possibleAnswers.Add(null);
            }

            for (int i = 0; i < possibleAnswers.Count; i++)
            {
                possibleAnswers _= EditorGUILayout.TextField("Answer " + (i+1), possibleAnswers*);*_

}
}
conversationScript.dialogues[iteration] = thisDialogue;
//Debug.Log("message " + iteration + " " + conversationScript.dialogues[iteration].currentMessage);
}
if (GUI.changed)
{
EditorUtility.SetDirty(conversationScript);
UnityEditor.SceneManagement.EditorSceneManager.MarkAllScenesDirty();
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
private void OnDisable()
{
#if UNITY_EDITOR
EditorUtility.SetDirty(conversationScript);
UnityEditor.SceneManagement.EditorSceneManager.MarkAllScenesDirty();
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
#endif
}
}
Is there something I am doing wrong?
Thank you!

Hi, this question is old already, but I figured it would be nice if someone answered it

You have 1 main issue in your script that makes the editor not work properly.
Editor scripts should not edit the desired data directly on the instance! You should instead use a Serialized Object to edit, update and save any changes to your inspected data. This is the documented “correct” practice by unity.

This way your object will have Ctrl+z functionality, save the asset, and won’t dirty the whole scene while you are editing

In your editor OnEnable you should do:

private SerializedObject dialogueAsset;
void OnEnable()
{
    dialogueAsset = new SerializedObject(target);
}

Then on your InspectorGUI function, you can access variables via dialogueAsset.FindProperty("propertyName") function

At the start of your InspectorGUI function you should also Update your serialized object data by calling:
dialogueAsset.Update()

And at the end of the function (after editing everything you want), you then call dialogueAsset.ApplyModifiedProperties() to tell unity to save the changes made in the editor!

Everything I said is how you usually should do simple editors like the one you showed off, but that said, there are plenty of situations where you would need to call Asset Database functions and set objects dirty, but that comes with experience :slight_smile: