Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
avatar image
0
Question by Julhoms · Jul 31, 2015 at 09:51 AM · unity5argumentoutofrangeexception

ArgumentOutOfRangeException: Argument is out of range. Parameter name: index

Hello,

I'm learning Unity and C# and trying to fix this error I get in this simple quiz game code.

When is this error displayed?

First question with 4 answers is being displayed nicely, but after user selects correct answer, second question is not loaded and I see error in the console.

I get this error:

ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[MathTopic+Question].get_Item (Int32 index) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) QuestionnaireNEW.OnGUI () (at Assets/test_01/QuestionnaireNEW.cs:73)

Error seems to be connected with this line of code in QuestionnaireNEW.cs:

         GUI.Label(new Rect((Screen.width / 2) - (200 / 2), (Screen.height / 2) - (25 / 2) - 75, 200, 200), mathList[index].QuestionText);

I have these .cs files:

QuestionnaireNEW.cs

MathTopic.cs

SpellingTopic.cs

I will paste code here for the QuestionnaireNEW.cs and MathTopic.cs, as these seem to be connected with the error I have.

QuestionnaireNEW.cs

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 
 public class QuestionnaireNEW : MonoBehaviour {
     
     //Inspector Settings....
     public int questionTextSize;
     public int buttonTextSize;
     public int buttonWidth;
     public int buttonHeight;
     public int index;
     
     //Topics....
     public int topic;
     
     
     public class Question
     {
         public string QuestionText;
         public List<string> Answers = new List<string>();
         public int CorrectAnswer;
     }
     
     //Define the topic lists....
     List<MathTopic.Question> mathList = new List<MathTopic.Question>();
     List<SpellingTopic.Question> spellingList = new List<SpellingTopic.Question>();
     
     void GetMathQuestion()
     {
         index = Random.Range(0, mathList.Count);
         Debug.Log("What is index? " + index);
         return;  
     }
     void GetSpellingQuestion()
     {
         index = Random.Range(0, spellingList.Count);
         return;
         
     }
     
     void Awake()
     {
         //Get a question at runtime.
         MathTopic.fill(mathList, mathList.Count);
         topic = 0;
         Debug.Log("Math Selected");
         
     }
     void OnGUI()
     {
         //--------------------------For Debug Purposes--------------------------------
         //GUI.Label(new Rect(10, 10, 100, 20), "Questions: " + list.Count);
         //GUI.Label(new Rect(10, 30, 200, 20), "Current Question: " + index.ToString());
         //GUI.Label(new Rect(10, 60, 200, 20), "Current Answers: " + list[index].Answers);
         //-----------------------------------------------------------------------------
         
         //Custom setings...
         GUI.skin.label.fontSize = questionTextSize;
         GUI.skin.button.fontSize = buttonTextSize;
         GUI.skin.label.alignment = TextAnchor.UpperCenter;
         GUI.skin.label.alignment = TextAnchor.UpperCenter;
         
         //This handles which topic to display
         //Answers from.
         switch (topic)
         {
         case 0:
 //            Debug.Log("Math");
             GetSpellingQuestion();
             //Display question...                                   
             GUI.Label(new Rect((Screen.width / 2) - (200 / 2), (Screen.height / 2) - (25 / 2) - 75, 200, 200), mathList[index].QuestionText);
             Debug.Log("Title is " + mathList[index].QuestionText);
             
             //Display the answers.
             for (int i = 0; i <= 3; i++)
             {
                 if (GUI.Button(new Rect((Screen.width / 2) - (buttonWidth / 2), (Screen.height / 2) - (25 / 2) + i * 30, buttonWidth, buttonHeight), mathList[index].Answers[i]))
                 {
                     //If correct answer...
                     if (i == mathList[index].CorrectAnswer)
                     {
                         Debug.Log("Correct");
                         
                         if (mathList.Count >= 1)
                         {
                             Debug.Log("Deleted question" + index.ToString());
                             mathList.Remove(mathList[index]);
                             GetMathQuestion();
                         }    //Here we would add the Ending...
                         else { Debug.LogError("Out Of Questions To Display"); }
                     }
                     else //if incorrect....
                     {
                         Debug.Log("Incorrect");
                         GetMathQuestion();
                     }
                 }
             }
             break;
         case 1:
             Debug.Log("Spelling");
             //Display question...                                   
             GUI.Label(new Rect((Screen.width / 2) - (200 / 2), (Screen.height / 2) - (25 / 2) - 75, 200, 200), spellingList[index].QuestionText);
             
             //Display the answers.
             for (int i = 0; i <= 3; i++)
             {
                 if (GUI.Button(new Rect((Screen.width / 2) - (buttonWidth / 2), (Screen.height / 2) - (25 / 2) + i * 30, buttonWidth, buttonHeight), spellingList[index].Answers[i]))
                 {
                     //If correct answer...
                     if (i == spellingList[index].CorrectAnswer)
                     {
                         Debug.Log("Correct");
                         
                         if (spellingList.Count >= 1)
                         {
                             Debug.Log("Deleted question" + index.ToString());
                             spellingList.Remove(spellingList[index]);
                             GetSpellingQuestion();
                         }    //Here we would add the Ending...
                         else { Debug.LogError("Out Of Questions To Display"); }
                     }
                     else //if incorrect....
                     {
                         Debug.Log("Incorrect");
                         GetSpellingQuestion();
                     }
                 }
             }
             break;
         case 2:
             break;
         }
         
         
         
         
         
     }
 //    void Update() { Debug.Log(mathList.Count); }
     
 }


MathTopic.cs

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 
 public class MathTopic : MonoBehaviour
 {
     
     public class Question
     {
         public string QuestionText;
         public List<string> Answers = new List<string>();
         public int CorrectAnswer;
     }
     
     static public void fill(@List<Question> list, int i)
     {
         //Questions and answers are listed here. Index beginning at 0.
         //The CorrectAnswer property should set to the correct
         //Index of the correct answer in this first one you can see
         //Correct answer is index "3". 0, 1, 2, and 3("Four Text").
         
         switch (i)
         {
         case 0:
             list.Add(new Question());
             list[0].QuestionText = "What is 2 + 2?";
             list[0].Answers.Add("1");
             list[0].Answers.Add("2");
             list[0].Answers.Add("22");
             list[0].Answers.Add("4");
             list[0].CorrectAnswer = 3;
             break;
         case 1:
             list.Add(new Question());
             list[1].QuestionText = "What is 4 + 4?";
             list[1].Answers.Add("4");
             list[1].Answers.Add("44");
             list[1].Answers.Add("8");
             list[1].Answers.Add("100");
             list[1].CorrectAnswer = 2;
             break;
             
         case 2:
             list.Add(new Question());
             list[2].QuestionText = "What is 6 + 6?";
             list[2].Answers.Add("16");
             list[2].Answers.Add("9");
             list[2].Answers.Add("12");
             list[2].Answers.Add("6,000,000");
             list[2].CorrectAnswer = 2;
             break;
         case 3:
             list.Add(new Question());
             list[3].QuestionText = "What is 8 + 8?";
             list[3].Answers.Add("2");
             list[3].Answers.Add("88");
             list[3].Answers.Add("5");
             list[3].Answers.Add("16");
             list[3].CorrectAnswer = 3;
             break;
         default:
             Debug.Log("Number out of range");
             return;
         }
         
     }
 }

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 Imankit · Jul 31, 2015 at 11:07 AM 0
Share

There is no ref to the mathlist therfore your mathlist will always be empty

Put this in questionnaireNew class -

 $$anonymous$$athTopic.fill(ref mathList, mathList.Count);

Put this in $$anonymous$$athTopic class -

  static public void fill( ref @List<Question> list, int i)
  {


avatar image Julhoms · Jul 31, 2015 at 11:14 AM 0
Share

@ankit.tiks007 Thank you for your reply. The thing is that the first question is being displayed nicely, but after I click the correct answer, the error is given that I described in my initial post, and second question is not being loaded.

avatar image troien · Jul 31, 2015 at 11:38 AM 1
Share

Your problem is that you only added one question to your mathList.

When you then remove that question fom your mathList, there areno more questionsto pick from.

So the next time you try to access a question, your index will be out of bounds. As there are no questions in your list, not even one. You should either check whether you are out of bounds:

 if(index >= 0 && index < mathList.Count)
     // Do something with mathList[index] here

Or make sure that there is more than one question in your list to begin with... Your fill method currently only adds one question, (as 'int i' is always 0) I believe you should just get rid of the switch...

A List already is a reference type, so as long as you don't say 'list = new List' anywhere in your method, you don't have to add 'ref' like @ankit.tiks007 said.

avatar image Julhoms · Jul 31, 2015 at 12:00 PM 0
Share

@troien Removing the switch worked! Now all the questions are being displayed. Thanks a lot!

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing one keyframe is changing all 0 Answers

shader_feature in "multiple shader program variants" doesn't work on Android 0 Answers

Unity 5 splat map replacement? 1 Answer

Blending in a simple shader not working in Unity5 as it used to work in Unity4 0 Answers

Change GIT branch in Unity 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