- Home /
SerializedProperty.DeleteArrayElementAtIndex leaves a null instead of removing an item.
Hi.
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?
I am seeing this behavior as well. The documentation helpfully says "this deletes the item at index," so no clue whether that is the intended behavior.
Answer by Jamora · Oct 29, 2013 at 05:28 PM
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)
sp.DeleteArrayElementAtIndex(i);
sp.DeleteArrayElementAtIndex(i);
There is nothing wrong with this answer, but it is worth saying that the current behaviour is useful if you are iterating through an array and deleting items, as the arraySize count stays valid.
Note that once the array item has been deleted, and the properties serialized back the asset, the array will not have the nulled item in it next time a SerializedObject is created from the asset.
All in all I don't think there is a need to null the items or fix the bug.
Answer by numberkruncher · Sep 22, 2014 at 03:53 PM
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;
arrayProperty.DeleteArrayElementAtIndex(i);
just a thought but I don't see where your decrementing the arraysize which you should do with each delete.
Probably necroing this but as the answer of @numberkruncher helped me today, I can affirm that there is no need to change the array size, it seems to be updated automatically.
I came across this a few years back and if my memory serves me correctly the Array is not actually resized but is copied and recontructed to a new Array. @Bunny83 totally shot down my short lived excitement on that one.
Your answer
