Index was out of range. Must be non-negative and less than the size of the collection.

Good day everyone. I’m still fairly new to unity, and as my first game I have been working on creating a visual memory card game.

I wanted to implement a restart button in my game that would reset the score count, however whenever I try to click on something in the scene that is NOT a card, I’m faced with an error, and I’m not sure what exactly is wrong with my code.

This is the part of my code in which I’m getting the error at:

    public Sprite board;

    public GameObject token;

    List<int> frontIndexes = new List<int>() { 0, 1, 2, 3, 0, 1, 2, 3 };

    public static System.Random random = new System.Random();
    public int shuffleNumber;

    SpriteRenderer spriteRenderer;

    void OnMouseDown()
    {
        int originalLength = frontIndexes.Count;
        float yPosition = 2.3f;
        float xPosition = -2.2f;

        if (spriteRenderer.sprite == board)
        {

            //creating this for loop in order to generate more clones
            for (int i = 0; i < 7; i++)
            {
                shuffleNumber = random.Next(0, frontIndexes.Count);
                Debug.Log(shuffleNumber);
                var temp = Instantiate(token, new Vector3(xPosition, yPosition, 0f),
                            Quaternion.identity);

                temp.GetComponent<FlippingCard>().frontIndex = frontIndexes[shuffleNumber];
                frontIndexes.Remove(frontIndexes[shuffleNumber]);

                xPosition = xPosition + 4;
                if (i == (originalLength/2 - 2))
                {
                    yPosition = -2.3f;
                    xPosition = -6.2f;
                }

            }
            token.GetComponent<FlippingCard>().frontIndex = frontIndexes[0];
        }
    }

The error I get is on line 36, where it says: temp.GetComponent<FlippingCard>().frontIndex = frontIndexes[shuffleNumber];

Error:

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection
Parameter name: index
System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <9577ac7a62ef43179789031239ba8798>:0)
System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <9577ac7a62ef43179789031239ba8798>:0)
System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <9577ac7a62ef43179789031239ba8798>:0)
MakeVisible.OnMouseDown () (at Assets/Scripts/MakeVisible.cs:36)

I’ve been trying since yesterday to come up with a solution however I was unable to fix it. I’d really appreciate the help!

When you remove the number you’ve just used you do this:

frontIndexes.Remove(frontIndexes[shuffleNumber]);

That won’t remove the index you want, it’ll remove the value contained at that index (in your code it might remove 0,1,2 or 3 rather than the index which could be between 0 and 7). You would need to do this instead:

frontIndexes.Remove(shuffleNumber);
1 Like

Since OP posted this twice, let’s move all further replies to the thread with more replies already:

1 Like

I tried this, and it gave me the same error. :frowning: I just want to be able to click the reset button however whenever I try to click something that is not a card, I’m getting this index out of range error.

After the first click, the list will be empty and no index is valid on an empty list.

Looks like you’re trying to access the item which is not there in a list.