Array Index is out of Range and I don't understand why.
I'm not entirely sure what Code to include in this. Basically Im working with a deck of cards. I created a scene to create the deck and shuffle it and generally act as a deck, and it all works fine. But when I created a new scene and used the deck as a prefab I started getting an IndexOutOfRangeException.
public void ToggleFace(bool showFace){
if (showFace) {
spriteRenderer.sprite = faces[cardIndex];
}
} else {
spriteRenderer.sprite = cardBack;
}
}
After doing some debugging I found out the exception happens always on the first 'card' that is being created. To be honest It looks as though other than the exception being thrown it all runs. But Im guessing that somehow an instance from the faces array is being called before its created, but I dont understand why since the faces array is filled in the inspector. What should I do?
EDIT: In another script I have:
void ShowCards(){
int cardCount = 0;
if (deck.HasCards) {
int k = 0;
foreach (Card i in deck.GetCards()) {
float co = cardOffset * cardCount;
Vector3 temp = start + new Vector3 (co, 0f);
i.transform.position = temp;
if (i.getColorSuit () == ColorSuit.Blue) {
if (i.getBidAction () == BidAction.None) {
if (i.getCardValue () == 3) {
k = 20;
}
if (i.getCardValue () == 4) {
k = 22;
}
if (i.getCardValue () == 5) {
k = 7;
}
}
if (i.getBidAction () == BidAction.Getaway) {
k = 19;
}
}
i.cardIndex = k;
i.ToggleFace (true);
SpriteRenderer spriteRenderer = i.GetComponent<SpriteRenderer> ();
spriteRenderer.sortingOrder = deck.CardCount () - cardCount;
cardCount++;
}
}
}
I cut a lot of the stuff for showing what causes k to be whatever number it is but you get the idea I think (k can be from 0-23 since that is how many card faces I have). And the error is pointing to where it gets called :
void Update(){
if (cardCounter != deck.CardCount ()) {
cardCounter = deck.CardCount();
ShowCards();
}
}
Sorry if the code given is formatted in a weird way. On my screen it looks like a paragraph but when I try to edit it it looks as it should :/
Without telling us where you assign a value to card index, there isn't much to do to help.
Sorry I'm new to unity. I updated the post to have everything I think you'd need. But the short answer, I think, is its being assigned in the update method in another script.
In both your codeblocks you have unequal amounts of curly brackets.
Copy and paste the code from your code editor. Highlight all the code without anything non-code highlighted between and click the 101010 button.
Dont use whitespace (spacebar) for indentation in your code editor. Always use the tab key.
Thanks! I was totally doing that backwards
Hello, What is the exact line (in ShowCars() i guess?) where the exception is thrown?
In the show cards method the error is thrown at i.ToggleFace. Like I mentioned in the question when in its own scene the whole thing works as it should but now in another scene it doesn't. And the error always gets thrown on the first card instanciated
Hm okay. Could you post the deck.CardCount() method? I think there could be the mistake because it probably counts the sprites in your faces array, right?
I posted the method, but I did use a faces.Length to debug and possibly somewhere else. Could you please explain what the problem with that is?