ArgumentOutOfRangeException: Index was out of range.

Hello! Sorry for bothering … I am having this error:

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

this is the piece of code where I am getting error:

else

        {
            for (int i = 0; i < lunghezzaCompleta; i ++)
            {
                if (!Database.database.marca[i].Contains(marca1Selezionata)) { listaDatabaseDaFiltrare1.Remove(i); }
                listaDatabaseFiltrato1 = listaDatabaseDaFiltrare1;
                Debug.Log("Valore di I: " + i);

            }

            Debug.Log("LISTA FILTRATO 1; " + listaDatabaseFiltrato1.Count);

            for (int x = 0; x < listaDatabaseFiltrato1.Count; x ++)
            {
                listaFiltrataModello1.Add(Database.database.modello[x]);
                Debug.Log("Valore di X: " + x);

            }
        }

Now … while in the console I am having all values of int i … it’s not the same for int x

listaDatabaseFiltrato1.Count is giving positive results (int numbers) … so I can’t figure out why I am having this error!
If i get: i = 0, i = 1, i = 2 and so on … why can’t int x be 0,1,2,3 ?

Thanks for helping me understanding!

One thing: you are using .Remove() on your list but you are giving it an index. I think that means you want .RemoveAt()

As for the two dereference points (Database.database.marca and Database.database.modello[×]), before you use them, just print either their .Count or their .Length values (depending if they are a List or an Array) as well as your i or x value, and see where it is going out of range.

Hi Kurt! Thank you for your reply!
:wink: Yes, I know It’s strange but … that’s a list of int numbers I use as index … It’s a sort of “list of index” that I want to remove. I Want to remove the value stored, not the index!

I did it!

Debug.Log("LISTA FILTRATO 1; " + listaDatabaseFiltrato1.Count);

is returning me a value … that’s why I can’t understand where is my mistake!

I sort it out … but now I have another problem!

I get that message because … database get cleared and I don’t know why! :smile: Well I know what to do this sunday!

Thanks a lot for your help!