Unknown Argument Out of Range Index Error On Card Game
I traced it with the debug log or by simply using print(string_value)
method to trace the remaining items within an index. Also, I locked in with an if condition to ask the system not to remove it if the content of the list is empty. Let's say after checked the end round result of the card game by checking its value and then discarding all items within the list object if it is not empty. Here's what I did:
/**
*
* Resets computation in hand for combo valid in hand before next player.
*
*/
public void ResetComboCheckInHand(int playerIndex) {
// Count Setup
countS = 0;
int cardCount = cardForStraightCheck[playerIndex].ToArray().Length;
// All cards are gone for the next batch.
for(int i = 0; i < cardCount; i++) {
print ("COUNT: " + i + " out of " + cardCount + " | " +
cardForStraightCheck[playerIndex].ToArray().Length + "-" +
cardOrder.ToArray().Length + "-" + suit.ToArray().Length);
if(((cardForStraightCheck[playerIndex].ToArray().Length > 0) &&
(cardOrder.ToArray().Length > 0) &&
(suit.ToArray().Length > 0))) {
cardForStraightCheck[playerIndex].RemoveAt(i); // --> This is where the bug stops
cardOrder.RemoveAt(i); // --> All card numbers collected
suit.RemoveAt(i); // --> All of their card suits
}
}
// Clear content and clean up.
cardForStraightCheck[playerIndex].Clear();
cardOrder.Clear();
suit.Clear();
}
Next, supposed these three lists (cardForStraightCheck, cardOrder, and suit) all contained 9 items respectively depending on the no. of cards in player's hand; where the first list is for card game object, the second list is for card's number value, and the third list is for identifying the suit type. Since I got the max index and supposed to be stopped at 8 but ended up on the 6th index where the error is identified via Unity Console.
COUNT: 0 out of 9 | 9-9-9
COUNT: 1 out of 9 | 8-8-8
COUNT: 2 out of 9 | 7-7-7
COUNT: 3 out of 9 | 6-6-6
COUNT: 4 out of 9 | 5-5-5
COUNT: 5 out of 9 | 4-4-4
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
Since the list objects had no empty item but the bug stopped at the first list object.
// This is where the cause of error red when performing this method.
System.Collections.Generic.List`1[UnityEngine.GameObject].RemoveAt (Int32 index) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:538)
// This is where the first list detected an error.
ComboCheck.ResetComboCheckInHand (Int32 playerIndex) (at Assets/Combo Module/ComboCheck.cs:1763)
I'm only use this method after done checking player's cards in hand before checking onto the next player
Answer by David_29 · Nov 20, 2015 at 09:15 AM
I decided to answer on my own since I found the solution after constant checking with my Unity console and trace the reason for receiving argument error on removing item depends on what index is. Removing all items one by one using the for loop is one of the basic methods for emptying all items on a list. The best answer to solve this error is to replace i
to 0
like this:
// BEFORE
cardForStraightCheck[playerIndex].RemoveAt(i);
cardOrder.RemoveAt(i);
suit.RemoveAt(i);
// AFTER
cardForStraightCheck[playerIndex].RemoveAt(0);
cardOrder.RemoveAt(0);
suit.RemoveAt(0);
Let's say the following items within a list contains only numbers like this:
ITEMS: 5 - 7 - 9 - 3 - 12 - 6 - 6
INDEX: 0 - 1 - 2 - 3 - 4 - 5 - 6
Now, here's what happen when using with the for loop and the next item to be removed using designated target index base on the index count from the for loop:
// FIRST LOOP - TARGET INDEX 0 (Where "index = i" from loop.)
ITEMS: 5 - 7 - 9 - 3 - 12 - 6 - 6
INDEX: 0 - 1 - 2 - 3 - 4 - 5 - 6
NEW: 7 - 9 - 3 - 12 - 6 - 6
// SECOND LOOP - TARGET INDEX 1 (Where "index = i" from loop.)
ITEMS: 7 - 9 - 3 - 12 - 6 - 6
INDEX: 0 - 1 - 2 - 3 - 4 - 5
NEW: 7 - 3 - 12 - 6 - 6
// THIRD LOOP - TARGET INDEX 2 (Where "index = i" from loop.)
ITEMS: 7 - 3 - 12 - 6 - 6
INDEX: 0 - 1 - 2 - 3 - 4
NEW: 7 - 3 - 6 - 6
// FOURTH LOOP - TARGET INDEX 3 (Where "index = i" from loop.)
ITEMS: 7 - 3 - 6 - 6
INDEX: 0 - 1 - 2 - 3
NEW: 7 - 3 - 6
// FIFTH LOOP - TARGET INDEX 4 (Where "index = i" from loop.)
ITEMS: 7 - 3 - 6
INDEX: 0 - 1 - 2
NEW: - - Error! There is no index no. 4! - -
See the point? This is how I got traced this bug for the missing array when trying to remove via loop. The reason for this solution is to target only at index 0 so that it ensures removing all item index available. Here's how this simple solution works:
// FIRST LOOP - TARGET INDEX 0 (Anonymous default integer parameter set.)
ITEMS: 5 - 7 - 9 - 3 - 12 - 6 - 6
INDEX: 0 - 1 - 2 - 3 - 4 - 5 - 6
NEW: 7 - 9 - 3 - 12 - 6 - 6
// SECOND LOOP - TARGET INDEX 0 (Anonymous default integer parameter set.)
ITEMS: 7 - 9 - 3 - 12 - 6 - 6
INDEX: 0 - 1 - 2 - 3 - 4 - 5
NEW: 9 - 3 - 12 - 6 - 6
// And so on...
Tip to remember: the most overwhelming bug that complicates tracing errors for the whole is sometimes created only one simple line of code. Understanding your code is like a map. You know where the directions and areas you made. Sometimes, you have to take no. of steps backwards to see how the conclusion is done. Broaden more on observing your code and think what does the closest possible the bug that causes your program to create hiccup before providing the best solution for this situation.