SetDirty not working in Custom Editor with Nested Custom Property drawer

Hi,
I am a bit lost:

I have written a custom editor which draws a serializable property sp, but all values I assign to sp vanish on play. Importantly, the serialized property is drawn with two nested custom property drawers.

  • I have added sp.Update() and sp.ApplyModifiedProperties() before and after modifying the serialized property sp in the editor script
  • The custom editor script includes EditorUtility.SetDirty(t) at the end as well as Undo.RecordObject(t, "xxx") on the target object t.

What could go wrong here?

Any help would be very much appreachiated!


The screenshot shows the custon editor. If I click play, the fields “nuts”, “berries” (…) disappear. Anything else that is not part of the seralized property stays.


Code for the custom editor

[CustomEditor(typeof(StimulusManager))]
public class StimulusManagerEditor : Editor
{
    void OnEnable()
    {
        _target = (StimulusManager) target;        
        _targetSerial = new SerializedObject(target);
     }

    public override void OnInspectorGUI()
    {
        // save changes from the editor in the game
        _targetSerial.Update();
        Undo.RecordObject(_target, "Define stimulus");

        // Draw the nested property field
         _featuresProperty = _targetSerial.FindProperty("NestedFeatures.Array.data[0]"); //Get the property
        EditorGUILayout.PropertyField(_featuresProperty, true); //draw the property

       // Keep changes
         _targetSerial.ApplyModifiedProperties();
        EditorUtility.SetDirty(_target);
        EditorApplication.update.Invoke();
}

The custom property drawer is this

using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer (typeof (NestedFeature))]
public class NestedFeatureDrawer : PropertyDrawer
{
     public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
    {
        SerializedProperty FeaturesProperty = property.FindPropertyRelative("Features");
        EditorGUI.BeginProperty(position, label, property);

        var indent = EditorGUI.indentLevel; //store old indent
        EditorGUI.indentLevel = 0; //new indent

        // HERE SOMETHING GOES WRONG, THE CONTENT OF THIS PART VANISHES ON PLAY
        for (int j=0; j < FeaturesProperty.arraySize; j++)
        {

            EditorGUILayout.PropertyField(FeaturesProperty.GetArrayElementAtIndex(j), GUIContent.none, true);
        }

        EditorGUI.indentLevel = indent; //reset indent

        EditorGUI.EndProperty();

    }   
}

What happens if you replace new SerializedObject(target); by serializeObject ?