editor does not recognize custom changes

I have this code that uses some editor to create controls:

   public override void OnInspectorGUI()
   {
       base.OnInspectorGUI();

       if (GUILayout.Button("Add obstacles"))
       {
           ArrayUtility.Add(ref m_Segment.obstaclePositions, 0.0f);
       }

       if (m_Segment.obstaclePositions != null)
       {
           int toremove = -1;
           for (int i = 0; i < m_Segment.obstaclePositions.Length; ++i)
           {
               GUILayout.BeginHorizontal();
               m_Segment.obstaclePositions[i] = EditorGUILayout.Slider(m_Segment.obstaclePositions[i], 0.0f, 1.0f);
               if (GUILayout.Button("-", GUILayout.MaxWidth(32)))
                   toremove = i;
               GUILayout.EndHorizontal();
           }

           if (toremove != -1)
               ArrayUtility.RemoveAt(ref m_Segment.obstaclePositions, toremove);
       }

       if (GUILayout.Button("Add platform"))
       {
           ArrayUtility.Add(ref m_Segment.platformPositions, 0.0f);
       }

       if (m_Segment.platformPositions != null)
       {
           int toremove = -1;
           for (int i = 0; i < m_Segment.platformPositions.Length; ++i)
           {
               GUILayout.BeginHorizontal();
               m_Segment.platformPositions[i] = EditorGUILayout.Slider(m_Segment.platformPositions[i], 0.0f, 1.0f);
               if (GUILayout.Button("-", GUILayout.MaxWidth(32)))
                   toremove = i;
               GUILayout.EndHorizontal();
           }

           if (toremove != -1)
               ArrayUtility.RemoveAt(ref m_Segment.platformPositions, toremove);
       }

       if (GUILayout.Button("Add decoration"))
       {
           ArrayUtility.Add(ref m_Segment.decorationPositions, 0.0f);
       }

       if (m_Segment.decorationPositions != null)
       {
           int toremove = -1;
           for (int i = 0; i < m_Segment.decorationPositions.Length; ++i)
           {
               GUILayout.BeginHorizontal();
               m_Segment.decorationPositions[i] = EditorGUILayout.Slider(m_Segment.decorationPositions[i], 0.0f, 1.0f);
               if (GUILayout.Button("-", GUILayout.MaxWidth(32)))
                   toremove = i;
               GUILayout.EndHorizontal();
           }

           if (toremove != -1)
               ArrayUtility.RemoveAt(ref m_Segment.decorationPositions, toremove);
       }
   }

but when adding platform, decoration etc the editor seems to fail to notice these changes, and does not save or ask me to save unless I change some more “standard” values in the editor, like a public int value. How to fix that?

ok found a solution:

  public override void OnInspectorGUI() {
    EditorGUI.BeginChangeCheck();
        // my usual code that I posted in the previous post
        if (EditorGUI.EndChangeCheck())
        {
            // Changes detected, mark the object as dirty
            EditorUtility.SetDirty(target);
        }
    }

Like you’ve noticed above you need to inform Unity whenever you change the values of an asset via code. Using the Undo class also handles this (on top of giving you Undo/Redo functionality).

Manually dirtying an asset isn’t needed if you handle the modification of values through SerializedProperty’s (which is what they exists for).

I’m not really sure what I need to serialize in this case

Most likely you’re already serialising what you need, though it’s not what I meant.

The docs explain their usage: Unity - Scripting API: SerializedProperty

You get the serialised property for any serialised members in your type. Then, assigning values via the serialised property will automatically handle dirtying and provide undo support.