Editor List Items Not Expandable Unless Serialized "Normally"

Hi there,
I’m trying to create a custom editor, and in doing so i’ve found an interesting behaviour.
I have a custom class “BaseEffect” which correctly serializes, and I can edit the individual items when using fields such as:
[SerializeReference] private List<BaseEffect> turnEndEffects;
However, I later wanted to change the ordering and display of a couple other details in the editor. So I hid it from the inspector, and intead used EditorGUILayout.PropertyField. However, now the elements aren’t editable. They still show up in the list, but there is no ‘expansion’ arrow to modify their fields.
Strangely enough however, if I remove the [HideInInspector] tag, they (expectedly) both show up, but now both have the correct editable fields. But obviosuly I only want the one, and to customise when it is displayed in the inspector.
I presume there is some extra detail I am missing, however I couldn’t find it from my searching.


As you can see, “On End Effects” shows no expansion, despite being implemented exactly the same as “Turn End Effects”.

using UnityEngine;
using UnityEditor;
using System;
using System.Collections.Generic;
using System.Linq;

[CustomEditor(typeof(Condition))]
public class ConditionEditor : Editor
{
    private string[] listNames = {"Apply","Turn Start","Turn End","End"};
    private string[] childClassNames;
    private int selectedListIndex = 0;
    private Dictionary<String, SerializedProperty> sProp;

    private void OnEnable()
    {
        // Find all classes that inherit from BaseEffect
        Type parentType = typeof(BaseEffect);
        List<Type> childTypes = AppDomain.CurrentDomain.GetAssemblies()
            .SelectMany(assembly => assembly.GetTypes())
            .Where(type => parentType.IsAssignableFrom(type) && type != parentType)
            .ToList();

        // Extract class names
        childClassNames = childTypes.Select(type => type.Name).ToArray();

        // Get extra serialized properties.
        sProp = new Dictionary<string, SerializedProperty>
        {
            { "canStack", serializedObject.FindProperty("canStack") },
            { "maxStack", serializedObject.FindProperty("maxStack") },
            { "permenant", serializedObject.FindProperty("permenant") },
            { "turnsActive", serializedObject.FindProperty("turnsActive") },
            { "onApplyEffects", serializedObject.FindProperty("onApplyEffects") },
            { "turnStartEffects", serializedObject.FindProperty("turnStartEffects") },
            { "turnEndEffects", serializedObject.FindProperty("turnEndEffects") },
            { "onEndEffects", serializedObject.FindProperty("onEndEffects") },
        };

    }

    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        // Display the drop-down list
        if (target is Condition condition)
        {
            serializedObject.Update();
          
            // Draw bools and condtional sections.
            GUILayout.BeginVertical();
            GUILayout.BeginHorizontal();
            bool canStack = sProp["canStack"].boolValue;
            sProp["canStack"].boolValue = EditorGUILayout.Toggle("Can Stack", canStack);
            if (canStack)
            {
                sProp["maxStack"].intValue = EditorGUILayout.IntField("Max Stack", sProp["maxStack"].intValue);
            }
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();
            bool permenant = sProp["permenant"].boolValue;
            sProp["permenant"].boolValue = EditorGUILayout.Toggle("Permenant", permenant);
            if (!permenant)
            {
                sProp["turnsActive"].intValue = EditorGUILayout.IntField("Turns Active", sProp["turnsActive"].intValue);
            }
            GUILayout.EndHorizontal();
            GUILayout.EndVertical();

            // Draw action lists
            EditorGUILayout.PropertyField(sProp["onApplyEffects"], true);
            EditorGUILayout.PropertyField(sProp["turnStartEffects"], true);
            EditorGUILayout.PropertyField(sProp["turnEndEffects"], true);
            EditorGUILayout.PropertyField(sProp["onEndEffects"], true);

            selectedListIndex = EditorGUILayout.Popup("Select List:", selectedListIndex, listNames);
            int selectedClassIndex = EditorGUILayout.Popup("Add Effect", -1, childClassNames);

            if (selectedClassIndex >= 0 && selectedClassIndex < childClassNames.Length)
            {
                // Create an instance of the selected child class
                Type selectedType = Type.GetType(childClassNames[selectedClassIndex]);
                if (selectedType != null)
                {
                    object newInstance = Activator.CreateInstance(selectedType);
                    switch (selectedListIndex)
                    {
                        case 0:
                            condition.OnApplyEffects.Add((BaseEffect)newInstance);
                            break;
                        case 1:
                            condition.TurnStartEffects.Add((BaseEffect)newInstance);
                            break;
                        case 2:
                            condition.TurnEndEffects.Add((BaseEffect)newInstance);
                            break;
                        case 3:
                            condition.OnEndEffects.Add((BaseEffect)newInstance);
                            break;
                    }
                }
            }

            serializedObject.ApplyModifiedProperties();
        }
    }
}

Have you thought about using UI Toolkit / UI Builder for custom editors? Way easier, more possibilities and less troublesome because it’s not “immediate GUI”.

FWIW, you can find derived types so much easier these days: Unity - Scripting API: TypeCache

Don’t know the actual solution to the problem though. I agree with the above though, UI toolkit is a lot easier to work with.

Thank you for the TypeCache suggestion that does simplify the code greatly.
Unfortunately it appears that either the UI Toolkit method has a separate issue (or perhaps the same one revealing itself?)

ArgumentException: Can't find array size property!
UnityEditor.UIElements.Bindings.SerializedObjectList.RefreshProperties (System.Boolean includeArraySize) (at <68ddc6fd093848298397957a5969af5e>:0)

At least now I get an error rather than just strange behaviour, not sure if I should be happy or not.
In this case. However I only get this error if the [HideInInspector] tag is used on the variable. Without it, it works fine, with the expandable drop downs. Now I did want to use the hide in inspector to stop them being displayed twice - so as a work around for now I’ll just not use DrawDefaultInspector(); and just do the entire editor using the UI Builder.

Conclusion for googlers: [SerializeReference] and [HideInInspector] has a weird interaction with lists when it comes to later manually adding the PropertyField in a custom editor. Tested in 2022.3.5f1