Editor script for scriptableobject not working.

Hello,

I wanted to add a way to select multiple enum values for an enum in my scriptable object and found out about enummaskfield, but edit scripts don’t seem to work with scriptable objects? I tested it with a monobehavior and it works, it’s just scriptable object not working. I’m not very familiar with editor scripting so let me know if there’s something special I need to do for scriptable objects.

using UnityEditor;

[CustomEditor(typeof(Action))]
public class EditorActionTarget : Editor {
    public override void OnInspectorGUI() {
        Action myTarget = (Action) target;
        myTarget.targetType = (TargetType) EditorGUILayout.EnumMaskField("Enums", myTarget.targetType);
    }
}

Thanks

The problem here is that you are writing directly to your target object’s fields instead of to it serialized data stream, so the Editor does not know there are changes that are dirty and need to be flushed to disk. You may find e.g., my answer to this question helpful, but in short, you should instead use the Editor.serializedObject property and the SerializedObject.FindProperty() method to access the serialized data stream for the field in question. The result could look something like this:

using UnityEditor;
 
[CustomEditor(typeof(Action))]
public class EditorActionTarget : Editor {
    static readonly GUIContent targetTypeLabel = new GUIContent("Enums");
    SerializedProperty targetType;
    void OnEnable {
        // note that the string passed here needs to match the name of the actual serialized field, in case it is a backing field for an accessor; e.g., it might be m_TargetType in your case
        targetType = this.serializedObject.FindProperty("targetType");
    }
    public override void OnInspectorGUI() {
        this.serializedObject.Update();
        EditorGUILayout.PropertyField(targetType, targetTypeLabel);
        this.serializedObject.ApplyModifiedProperties();
    }
}

If you need or want to draw that property using a special type of control, the best way to do so is to use a custom PropertyAttribute and PropertyDrawer. Done properly, this can often obviate the need to make a custom Editor at all.