- Home /
Array index is out of range
Apologies if there's already an answer to this, it's probably something obvious but I can't work it out. I'm getting an Array Index Out Of Range error on one of my lists.
Answer by TanselAltinel · Apr 22, 2018 at 06:23 PM
2 solutsions:
Make answerObjects variable private
Remove answ1, ans2, ans3, ans4 and assign them to answerObjects fields in the editor, and remove Start() function.
1 is giving me an error. Can I have an example of what the code would look like for #2 to initialise the list please?
Code should be like this and objects answ1, ans2, ans3, ans4 should be assigned to answerObjects in the editor inspector area.
public Text$$anonymous$$esh[] answerObjects;
void Start () {
}
public void procedure() {
//some preceeding code
int correct = rnd.Next (0, 4);
int incorrectIndex = 1;
for (i = 0; i < 4; i++) {
if (correct == i) {
answerObjects [i].tag = "correct";
answerObjects [i].text = answers [0].ToString ();
} else {
answerObjects [i].tag = "incorrect";
answerObjects [i].text = answers [incorrectIndex].ToString();
incorrectIndex += 1;
}
}
Your answer
Follow this Question
Related Questions
array problem 1 Answer
Array index is out of range error Edited 1 Answer
Matching Index of two Arrays after one Array is sort 3 Answers
IndexOutOfRangeException: Array index is out of range. 1 Answer
Arrays rebels to my power! 1 Answer