Id counter wont increase, error code ArgumentOutOfRangeException: Index was out of range

I am trying to make an ID counter for icons and insert it in a list and I am getting this error for some reason, I have made sure that the ID counter is accessible, and that the code works until the line that inserts the icon.

[HideInInspector] public int idCounter = 0
    [HideInInspector] public List<Icon> Icons = new List<Icon>(1);

    public void ConfirmCurrentIcon()
    {
        Debug.Log(idCounter);
        Icons[idCounter] = icon;
        idCounter++;
        Debug.Log(idCounter);

}[/code]

This is the console:

If further information is required please ask for it

Thanks in advance:)

List aren’t arrays. An array would allow you to allocate a bunch of slots and then you can assign to those slots. But list require you to add or remove slots. Index out of range is because Icons[idCounter] doesn’t exist, thus it’s out of range. So either use an array or add to the list.

1 Like