Skip to content
Discussion options

You must be logged in to vote

If the list elements themselves are [SerializeReference] values, an add callback can explicitly initialize the new slot instead of leaving the inserted value in place. Unity documents the value after InsertArrayElementAtIndex as undefined, so it should be assigned explicitly: API reference.

For a custom Inspector/add handler, the relevant part is:

static void AddEmptyReference(SerializedProperty listProperty)
{
    int index = listProperty.arraySize;
    listProperty.InsertArrayElementAtIndex(index);
    listProperty.GetArrayElementAtIndex(index).managedReferenceValue = null;
    listProperty.serializedObject.ApplyModifiedProperties();
}

The new row then starts empty so its subtype can be…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by mackysoft
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants