ArgumentOutOfRangeException Error in my game
I am trying to build a quiz game. I got two scripts, a Question Data and a Question Manager script. In my QuestionManager script i keep getting this error when i try to set a new question. I've tried to resolve it but i can't seem to get it fixed. Error ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[Question].get_Item (Int32 index) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/EqualityComparer.cs:84) QuestionManager.SetNewQuestion () (at Assets/QuestionManager.cs:30) QuestionManager.Start () (at Assets/QuestionManager.cs:21)
this is my QuestionManager script
public class xxxxxxx {
[SerializeField]
private TextAsset questionDataXMLFile;
public QuestionData questionData;
private Question currentQuestion;
[SerializeField]
private Text factText;
void Start() {
questionData = QuestionData.LoadFromText(questionDataXMLFile.text);
factText = GetComponent <Text>();
SetNewQuestion ();
}
public void SetNewQuestion()
{
int index = Random.Range(0, questionData.questions.Count - 1);
Debug.Log (index);
currentQuestion = questionData.questions[index];
factText.text = currentQuestion.questionText
}
// Use this to see if user selected correct answer
public bool CorrectAnswerSelected(int selectedAnswerID) {
return selectedAnswerID == currentQuestion.correctAnswerID;
}
}
Answer by Jessespike · May 27, 2016 at 06:10 PM
Are the questions even loading? How many are being loaded? You didn't share how QuestionData is structured, so it's hard to say if anything is wrong there.
Debug.Log( questionData.questions.Count );
Don't think you should be subtracting 1 here:
int index = Random.Range(0, questionData.questions.Count - 1);
it should probably be:
int index = Random.Range(0, questionData.questions.Count);
Yes i made a mistake in the X$$anonymous$$L file and the questions were not being loaded..thanks for answering my question!
Answer by Tasarran · May 27, 2016 at 06:06 PM
You are referring to questionData.questions[], when you never define questionData as containing a questions[] array.
Your answer
![](https://koobas.hobune.stream/wayback/20220612081731im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
ArgumentNullException When using Collider2D.OverlapCollider() 1 Answer
OpenCVForUnity 0 Answers