How do I populate an empty list?

I have a typical struct and a typical call for it. But I have no idea how to add an empty item to the list, when the list itself is empty (not even appearing).

public struct XYZ {}

public class AAA : MonoBehaviour {
    public List<XYZ> CBA;

    void Start() {
        //CBA = new List<XYZ>();
    }

None of them appear in Inspector. I don’t know how to add an item. I need it to appear in Inspector so I can add elements with far less mess and link them into slots.

Unity serialize does not support List. Use an Array

List work just fine in the inspector, just make sure you add the System.Serializable to the struct, then when the script is added to a GameObject, you can then see the list and change the size and add values to the elements.

[System.Serializable]
public struct XYZ
{
    public string aString;
    public float aFloat;
}
4 Likes

That’s it.

Cool when was that added? Pretty sure it didn’t work when I first set out with our game in 5.2

Unity 4.5.

https://blogs.unity3d.com/2014/06/24/serialization-in-unity/

1 Like

As an extra note never make Lists public available for encapsulation reasons.

IEnumerables could be fine, but most often you want to abstract such things and have public methods that makes sense for what problem the class encapsulate