-1

i was trying to make a List of different kinds of gathering blocks. But i wanted to make it so that there was only 1 in the inspector and then seperate them by category in code. I wanted to do this so the inspector did not get cluttered with different lists.

However after setting it up, i kept getting a error saying ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index System.Collections.Generic.List`1[T].set_Item (System.Int32 index, T value) (at <9aad1b3a47484d63ba2b3985692d80e9>:0) BlockPlacing.Start () (at Assets/m/Scripts/BlockPlacing.cs:16)

My code/inspector list looks like this:

Inspector List Image

Code:

    [SerializeField] private List<GameObject> blocks = new List<GameObject>(64);
    private List<List<GameObject>> _mineralsList = new List<List<GameObject>>(4);

    void Start()
    {
        for (int j = 0; j < 4; j++)
        {
            for (int i = 0; i < 4; i++)
            {
                _mineralsList[j].Insert(i, blocks[i]);
            }
        }
    }

A different way of adding to the _mineralList i saw and tried was _mineralsList[j][i] = blocks[i] But that also didn't work and gave the same error.

  • Where you do initialize each element of your outer list? – gunr2171 Feb 14 '23 at 13:16
  • The parameter that you pass to your list contructors doesn't mean that you have already that number of elements in the list. So when you write __mineralsList[j]_ and j is zero you get that error. First you need to Add some elements in that list – Steve Feb 14 '23 at 13:21
  • In the same way the Insert called at index 0 when there is no element in the list will cause next a NullReferenceException. You need to create the inner list before trying to use any of its methods. – Steve Feb 14 '23 at 13:23
  • @Steve ```_mineralsList[j] = new List(4);``` So i added this in the outer forloop. So it looks like this: for (int j = 0; j < blocks.Capacity / 4 / 4; j++) { _mineralsList[j] = new List(4); for (int i = 0; i < blocks.Capacity / 4 / 4; i++) { _mineralsList[j].Insert(i, blocks[i]); } } But it still gives the same error. I think this is what you meant? – Bloemenpot Feb 14 '23 at 13:25
  • And _blocks[i]_ when i == 0? It doesn't exist an element of type GameObject at index 0 of that blocks list. Is the same as the previous problem 64/4 are just hints to the undelying list constructor that you expect to add 64/4 elements. It is not a request to create 64/4 GameObjects and insert them in the list – Steve Feb 14 '23 at 13:34
  • @Steve im 99.9% certain that blocks[i] is not empty. Since when i Debug.Log(blocks[i]) it gives the GameObject that i inserted in the unity inspector. – Bloemenpot Feb 14 '23 at 13:37

1 Answers1

0

try to use this code:

public void Start()
{
    for (int j = 0; j < 4; j++)
    {
        _mineralsList.Add(new List<GameObject>());
        for (int i = 0; i < 4; i++)
        {
            if (i < blocks.Count) {
                _mineralsList[j].Add(blocks[i]);
            }
        }
    }
}
  • Hey, this worked. When i saw you're answer i immediately noticed what i was missing. I tried making the inner list in a way i would instantiate any object. But it has to be added since it is a list haha. Thank you very much! :D – Bloemenpot Feb 14 '23 at 14:20
  • you're welcome @Bloemenpot, please vote my anwser if it is usefully ;-) – Luca La Malfa Feb 16 '23 at 16:19
  • 1
    Hey, i tried to but i need atleast 15 reputation to upvote the answer, sorry :( – Bloemenpot Feb 18 '23 at 00:34