List of List

Hi all , i´m trying to initialize one main list that contain another list and other datas inside (List of List). When a start the program I receive the follow error :
“ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.”

This error is because I not inicializate the main list, If i do it manually on inspector and create 6 lines (exemple on image), the program starts fine. My question is, how can I inicializate this list by code ? I´m thinking something like listTypeslist = new List<…

    public class ListTypes
    {
        public string nameOfList;
        public string genderOfList;
        public string scenarioOfList;
        public List<string> listTypes;
    }

    [System.Serializable]
    public class types
    {
        public List<ListTypes> typesList;
    }

    public types listTypeslist = new types();

Thanks

For lists, you need to use .Add repeatedly to get the list items you need.

List<ListTypes> listTypesList = new List<ListTypes>(6);
for (int x=0; x<6; x++) {
listTypesList.Add(new ListTypes());
}

Sidenote: The (6) in the first line isn’t strictly necessary (you could do this without any parameters), but it’s a good habit if you know ahead of time the approximate end size of the list, as you do here. It sets the capacity of the list; if you don’t use that, then the list will have to resize itself a few times as you add more items, and each resize operation takes some work as it copies the list items to the new array. This is inconsequential on a list of size 6, but if you have a longer list of hundreds of items, it can become a major performance drain.

Thanks StarManta, your solution worked fine here , I only changed one line
for (int x = 0; x < 6; x++)
{
listTypeslist.typesList.Add(new ListTypes());
}

[ ]´s

Another way to do this is using the collection initializer syntax:

public types listTypeslist = new types
(
    new List<ListTypes>()
    {
        new ListTypes("1", "", "", new List<string> { "" }),
        new ListTypes("2", "", "", new List<string> { "" }),
        new ListTypes("3", "", "", new List<string> { "" }),
        new ListTypes("4", "", "", new List<string> { "" }),
        new ListTypes("5", "", "", new List<string> { "" }),
        new ListTypes("6", "", "", new List<string> { "" })
    }
);