Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by David_29 · Nov 20, 2015 at 03:30 AM · c#arraylistindexargumentoutofrangeexception

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

37 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to create a Matrix / Array / List / Grid of gameObjects? 1 Answer

[help] Array index is out of range 0 Answers

Get all Children of an Object with a certain component 0 Answers

Logic problem with lists C# 2 Answers

How to get all children of a Gameobject with a certain component 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges