Adding item to List with multiple objects selected throws error when accessing SerializedProperty

I think this problem may be a little too confusing to explain in text, so I made a video to show the problem and explain everything I’ve tried. Hopefully someone is familiar with this error. I’ve been stuck on this error for a couple days and haven’t found a single answer online or through my troubleshooting.

Here’s the error:

InvalidOperationException: The operation is not possible when moved past all properties (Next returned false)
UnityEditor.SerializedProperty.Verify (UnityEditor.SerializedProperty+VerifyFlags verifyFlags) (at C:/buildslave/unity/build/Editor/Mono/SerializedProperty.bindings.cs:330)
UnityEditor.SerializedProperty.FindPropertyRelativeInternal (System.String propertyPath) (at C:/buildslave/unity/build/Editor/Mono/SerializedProperty.bindings.cs:1320)
UnityEditor.SerializedProperty.FindPropertyRelative (System.String relativePropertyPath) (at C:/buildslave/unity/build/Editor/Mono/SerializedProperty.bindings.cs:204)

Somehow the object is not being registered as dirty so serializedObject.Update isn’t doing anything.

It’s still not completely clear what causes this issue. From what I can tell, it happens when you don’t change a field through a SerializedProperty while having multiple objects selected. If you don’t use the SerializedProperty, the SerializedObject isn’t marked as dirt so calling serializedObject.Update doesn’t do anything. You need to call serializedObject.SetIsDifferentCacheDirty() before calling Update which will mark it dirty and in-turn make Update work properly.

For everyone who stuck finding the cause of this error:

In my case this was caused by iterating over child properties of cached SerializedProperty like this way:

private SerializedProperty _cachedParentProperty;

    void OnEnable()
    {
        _cachedParentProperty = new SerializedObject(myScriptableObject).FindProperty("_myProperty");
    }

    void OnGUI()
    {
        foreach (SerializedProperty childProperty in _cachedParentProperty)
        {
            EditorGUILayout.PropertyField(childProperty, true);
        }
    }

I still no clue why it is happening but getting child properties using FindProperty of SerializedObject inside OnGUI instead of referencing the cached one was solved the issue for me.

private SerializedObject _serializedObject;

    void OnEnable()
    {
        _serializedObject = new SerializedObject(myScriptableObject);
    }

    void OnGUI()
    {
        foreach (SerializedProperty childProperty in _serializedObject.FindProperty("_myProperty"))
        {
            EditorGUILayout.PropertyField(childProperty, true);
        }
    }

You might try making those cached properties public or marking them with the SerializeField attribute. If the Editor Window gets rebuilt at any point (it’ll happen) those refs would be lost.

Had this problem as well and the solution (regardless of if you’re using a cached property or not) is to call:

serializedObject.SetIsDifferentCacheDirty();

This was important for my use-case as the button I was clicking was causing each object to be updated with a unique value and the serialized property wasn’t getting the memo that the selected objects now had multiple values.

1 Like