C# Index Out of Range Exception When the Index Is Within the Length of the Array
I have a public Sprite array that I use to access the sprites I need. This works fine and when I debug it It prints out all of the names of the sprites that it should. When I debug this array for length, it returns 9, which is appropriate. The line I have a problem on is as follows: hexagon.GetComponent<SpriteRenderer>().sprite = hex.hexColors[1];
where "hexagon" is a GameObject, hex is my Hexagon script, and hexColors is my Sprite array from earlier with length 9. I am getting the error: "IndexOutOfRangeException: Array index is out of range." I don't understand how this could be the case considering that the length is 9 and 1 is between 0 and 9.
Where is that array initialized and where is this line being called?
$$anonymous$$ake sure you are debugging the hexColors array (check length) immediately before the line throwing the error. If you are debugging at the time of initialization ins$$anonymous$$d of at the time it is used, something may have changed since.
Did you forget to use GetComponent to grab your 'hex'?
Your answer
![](https://koobas.hobune.stream/wayback/20220612153523im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Convert Keyboard controlls to UI Buttons 1 Answer
Performance question: Is it better to Instantiate particle systems OR use same particle system? 1 Answer
How can I change 'Order in Layer' 1 Answer
why the jump doesn't works? 1 Answer
I need a clean way to “cast” cards in a card game in Unity? 0 Answers