SerializedProperty.DeleteArrayElementAtIndex leaves a null instead of removing an item.


I got a strange problem.

I have a custom inspector script which manipulates a SerializedProperty array. What I noticed is that when I use DeleteArrayElementAtIndex it doesn’t actually delete an item but leaves a null at a specified index.

I added a second check which is triggered during the next Layout event which looks for null values and does the same DeleteArrayElementAtIndex on the first one. And this time array actually shrinks.

Has anyone experienced such behavior?

Another solution is to assign a value of null to objectReferenceValue prior to deleting it. This has the advantage of not deleting multiple elements if Unity decide to fix this issue in the future:

// Unity doesn't remove element when it contains an object reference.
var elementProperty = arrayProperty.GetArrayElementAtIndex(i);
if (elementProperty.objectReferenceValue != null)
    elementProperty.objectReferenceValue = null;


I did a few tests, and my results are that the index is deleted (and the array shrunk) if the element is null. If it is not null, then it is set null.

So if you want to shrink the array by one, then code like this seems to do the trick:

SerializedProperty sp ;

if(sp.GetArrayElementAtIndex(i).objectReferenceValue != null)