Custom Inspector doesn't show changes immediatley

Hi,

I’m using the custom editor for managing reactions from this tutorial: Adventure Game Phase 4: Reactions - Unity Learn

The reactions are ScriptableObjects collected in an array.

I wanted to add the functionality to move the Reaction’s position in the array. I tried to realize this with the use of two buttons (up/down) per reaction (similar to how a Reaction is deleted from the Array; tutorial video at 23:10).

The methods are called from OnInspectorGUI:

public override void OnInspectorGUI ()
    {
        serializedObject.Update ();
        //Move element
        if (GUILayout.Button("^", GUILayout.Width(buttonWidth)))
        {
            reactionsProperty.MoveElementArray(reaction, false);
        }
        if (GUILayout.Button("v", GUILayout.Width(buttonWidth)))
        {
            reactionsProperty.MoveElementArray(reaction, true);
        }

        //Delete Element
        if (GUILayout.Button ("-", GUILayout.Width (buttonWidth)))
        {
            reactionsProperty.RemoveFromObjectArray (reaction);
        }

        serializedObject.ApplyModifiedProperties();
    }

This is the method that moves the elements:

    public static void MoveElementArray<T> (this SerializedProperty arrayProperty, T elementToMove, bool direction)
        where T: Object
    {
        arrayProperty.serializedObject.Update();

        for (int i = 0; i < arrayProperty.arraySize; i++)
        {
            SerializedProperty elementProperty = arrayProperty.GetArrayElementAtIndex(i);

            if (elementProperty.objectReferenceValue == elementToMove)
            {
                //if direction is false the element is moved to index -1
                if (!direction)
                {
                    arrayProperty.MoveArrayElement(i, i - 1);
                }
                //if direction is false the element is moved to index + 1
                else
                {
                    arrayProperty.MoveArrayElement(i, i + 1);
                }

                arrayProperty.serializedObject.ApplyModifiedProperties();
                return;
            }
        }
    }

The method works, but the changes are only shown when I leave and re-enter the inspector (when I click somewhere else and then again on the object that contains the Reactions).
When using the Delete-Button (Delete Array-Element) the information in the inspector changes instantly, but when the Move-Buttons are pushed it doesn’t. I don’t understand why this would work differently.

Any thoughts?

You have two serializedObject.Update and two serializedObject.ApplyModifiedProperties in your code (actually 3 but only two are called the same iteration.)

Update should be called once at the beginning of your custom inspector and ApplyModifiedProperties once at the end.

Having the same issue with almost the same code, but no inmediate change at all. I have to change GO and return to the reaction to see the changes. I expected it to work the same way as removing objects, but wasn’t the case.

Any new ideas on this regard?