• 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 /
This question was closed Feb 12, 2017 at 03:54 PM by Eagle-Bound for the following reason:

I rewrote the code and it fixed itself :P

avatar image
0
Question by Eagle-Bound · Feb 11, 2017 at 05:24 PM · c#listnoob

Argument Out Of Range Parameter Name: Index

Hi, So I have a script and I'm getting the error Argument Out Of Range Parameter Name: Index, but I don't know why. I've done some looking around, and I've seen people saying their list or something has nothing in it, which would make sense, but to my knowledge, I don't. So I know this is kind of a silly question, but if anyone could help me it would be much appreciated. Here's my code:

 public void MultChoice(){
         if (multChoice.Count > 0) {
             multChioce1.GetComponentInChildren<Image> ().color = Color.white;
             multChioce2.GetComponentInChildren<Image> ().color = Color.white;
             multChioce3.GetComponentInChildren<Image> ().color = Color.white;
             multChioce4.GetComponentInChildren<Image> ().color = Color.white;
 
             correctButtonNumber = (int)UnityEngine.Random.Range (0, 3);
 
             tempIntMult = UnityEngine.Random.Range (0, multChoice.Count);
 
             randomizedTextMult.text = hir2 [tempIntMult];
 
             int one = UnityEngine.Random.Range (0, rom2.Count);
             rom2.RemoveAt (one);
             int two = UnityEngine.Random.Range (0, rom2.Count);
             rom2.RemoveAt (two);
             int three = UnityEngine.Random.Range (0, rom2.Count);
             rom2.RemoveAt (three);
             int four = UnityEngine.Random.Range (0, rom2.Count);
             rom2.RemoveAt (four);
 
             switch (correctButtonNumber) {
             case 0:
                 multChioce1.GetComponentInChildren<Text> ().text = rom2 [tempIntMult];
                 multChioce2.GetComponentInChildren<Text> ().text = rom2 [two];
                 multChioce3.GetComponentInChildren<Text> ().text = rom2 [three];
                 multChioce4.GetComponentInChildren<Text> ().text = rom2 [four];
 
                 rom2.Add (multChioce1.GetComponentInChildren<Text> ().text);
                 rom2.Add (multChioce2.GetComponentInChildren<Text> ().text);
                 rom2.Add (multChioce3.GetComponentInChildren<Text> ().text);
                 rom2.Add (multChioce4.GetComponentInChildren<Text> ().text);
                 break;
             case 1:
                 multChioce1.GetComponentInChildren<Text> ().text = rom2 [one];
                            // I think the error is on the line above.
                 multChioce2.GetComponentInChildren<Text> ().text = rom2 [tempIntMult];
                 multChioce3.GetComponentInChildren<Text> ().text = rom2 [three];
                 multChioce4.GetComponentInChildren<Text> ().text = rom2 [four];
 
                 rom2.Add (multChioce1.GetComponentInChildren<Text> ().text);
                 rom2.Add (multChioce2.GetComponentInChildren<Text> ().text);
                 rom2.Add (multChioce3.GetComponentInChildren<Text> ().text);
                 rom2.Add (multChioce4.GetComponentInChildren<Text> ().text);
                 break;
             case 2:
                 multChioce1.GetComponentInChildren<Text> ().text = rom2 [one];
                 multChioce2.GetComponentInChildren<Text> ().text = rom2 [two];
                 multChioce3.GetComponentInChildren<Text> ().text = rom2 [tempIntMult];
                 multChioce4.GetComponentInChildren<Text> ().text = rom2 [four];
 
                 rom2.Add (multChioce1.GetComponentInChildren<Text> ().text);
                 rom2.Add (multChioce2.GetComponentInChildren<Text> ().text);
                 rom2.Add (multChioce3.GetComponentInChildren<Text> ().text);
                 rom2.Add (multChioce4.GetComponentInChildren<Text> ().text);
                 break;
             case 3:
                 multChioce2.GetComponentInChildren<Text> ().text = rom2 [one];
                 multChioce2.GetComponentInChildren<Text> ().text = rom2 [two];
                 multChioce2.GetComponentInChildren<Text> ().text = rom2 [three];
                 multChioce4.GetComponentInChildren<Text> ().text = rom2 [tempIntMult];
 
                 rom2.Add (multChioce1.GetComponentInChildren<Text> ().text);
                 rom2.Add (multChioce2.GetComponentInChildren<Text> ().text);
                 rom2.Add (multChioce3.GetComponentInChildren<Text> ().text);
                 rom2.Add (multChioce4.GetComponentInChildren<Text> ().text);
                 break;
             }
         } else {
             randomizedTextMult.text = "Done";
             multChioce1.GetComponentInChildren<Text> ().text = "";
             multChioce2.GetComponentInChildren<Text> ().text = "";
             multChioce3.GetComponentInChildren<Text> ().text = "";
             multChioce4.GetComponentInChildren<Text> ().text = "";
         }
     }

and in a separate script,

 public void CheckAnswerMultipleChoiceButtonOne ()
     {
         int hirTemp = hManager.hir2.IndexOf (hManager.randomizedTextMult.text);
         int romTemp = hManager.rom2.IndexOf (hManager.multChioce1.gameObject.GetComponentInChildren<Text> ().text);
 
         if (hirTemp == romTemp) {
             hManager.randomizedArray.RemoveAt (hManager.tempIntMult);
             hManager.MultChoice ();
                     //Or here?
             hManager.attempts = 0;
         } else {
             hManager.attempts++;
             if (hManager.attempts == 3) {
                 switch (hManager.correctButtonNumber) {
                 case 0:
                     hManager.multChioce1.GetComponentInChildren<Image> ().color = Color.green;
                     if (hirTemp == romTemp) {
                         hManager.randomizedArray.RemoveAt (hManager.tempIntMult);
                         hManager.MultChoice ();
                         hManager.attempts = 0;
                     }
                     break;
                 case 1:
                     hManager.multChioce2.GetComponentInChildren<Image> ().color = Color.green;
                     if (hirTemp == romTemp) {
                         hManager.randomizedArray.RemoveAt (hManager.tempIntMult);
                         hManager.MultChoice ();
                         hManager.attempts = 0;
                     }
                     break;
                 case 2:
                     hManager.multChioce3.GetComponentInChildren<Image> ().color = Color.green;
                     if (hirTemp == romTemp) {
                         hManager.randomizedArray.RemoveAt (hManager.tempIntMult);
                         hManager.MultChoice ();
                         hManager.attempts = 0;
                     }
                     break;
                 case 3:
                     hManager.multChioce4.GetComponentInChildren<Image> ().color = Color.green;
                     if (hirTemp == romTemp) {
                         hManager.randomizedArray.RemoveAt (hManager.tempIntMult);
                         hManager.MultChoice ();
                         hManager.attempts = 0;
                     }
                     break;
                 }
             }
         }
     }


Thanks!

Comment
Add comment · Show 4
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
avatar image Nomenokes · Feb 11, 2017 at 06:10 PM 0
Share

Which line is the error co$$anonymous$$g from?

avatar image Eagle-Bound Nomenokes · Feb 11, 2017 at 06:48 PM 0
Share

Line # 36 in the first script or Line # 8 in the second one? I think so, at least.

avatar image Nomenokes Eagle-Bound · Feb 11, 2017 at 07:02 PM 0
Share

Ah yeah sorry didn't see that comment the first time.

I think it's because you remove the item at "one" (line 15) and then try to access it in the error spot.

That might be wrong, since I'm not familiar with lists in c sharp.

avatar image RobAnthem · Feb 11, 2017 at 06:38 PM 0
Share

If an index is empty, it will return null. At some point in your code, you are referencing an index that does not exist. If it is a list or array staying confined is easy as this.

 if (myList[index] != null)
 {
 }
 if (index < myList.Count)
 {
 }
 if (index < myArry.Length)
 {
 }
 if (myArray[index] != null
 {
 }

1 Reply

  • Sort: 
avatar image
0

Answer by AurimasBlazulionis · Feb 11, 2017 at 07:42 PM

FIrst, do a check if rom2.Count > 0. Then if it is true, proceed. In all of your random.range, change the second parameter from rom2.Count to rom2.Count - 1. That is because the last element is always the number of elements inside the list minus one.

Comment
Add comment · Show 2 · 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
avatar image RobAnthem · Feb 11, 2017 at 07:45 PM 0
Share
 rom2.Count - 1

for Random.Range() would be improper, Unity's random class will never pick the max range.

     rom2.Count 

is adequate for random.

avatar image Eagle-Bound · Feb 11, 2017 at 07:59 PM 0
Share

I tried that, and I didn't work. I'm still getting the error, and the questions are starting to be messed up, so I could get four possible answers, none of which being the correct one. (BTW, this is a quiz game. Did I say that already? I don't think so.)

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

290 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 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 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 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 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 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 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 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

A node in a childnode? 1 Answer

floats and lists 1 Answer

Detecting OnMouseDown from colliders in an array 1 Answer


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