- Home /
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;
}
}
}
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)
{
@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.
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.
@troien Removing the switch worked! Now all the questions are being displayed. Thanks a lot!