I am creating a very basic SO with a single public List inside:
…and I am creating a custom editor script with a reordable list for it. Below is the simplified editor script I am using:
[CustomEditor(typeof(ItemsManager))]
public class ItemsManagerEditor : UnityEditor.Editor
{
private SerializedProperty items;
private ReorderableList reorderableList;
private void OnEnable()
{
items = serializedObject.FindProperty(nameof(ItemsManager.Items));
reorderableList = new ReorderableList(serializedObject, items, true, true, true, true)
{
onAddCallback = AddItem,
};
}
private static void AddItem(ReorderableList list)
{
var index = list.serializedProperty.arraySize;
list.serializedProperty.arraySize++;
list.index = index;
}
public override void OnInspectorGUI()
{
serializedObject.Update();
reorderableList.DoLayoutList();
serializedObject.ApplyModifiedProperties();
}
}
This is how the SO is visualized in the inspector:
The problem is that Items
is not initialized. If I try to click the [+] to add an item it gives me null reference on line 19 in the Editor script for the serializedProperty or the Items list in ItemsManager
.
I used same approach in other projects, same unity version but all was working as expected. The following screenshot is of a similar script but different project, where everything works correctly:
The only difference I see is the little “Serializable” info that Rider is showing to the right of the list items
. I wonder why the Items
from the ItemsManager
is not showing as Serializable? Maybe this is the reason it does not get initialized.
Maybe there are some Editor settings regarding serialization? Any help would be appreciated.