x


Array Index is Out of Range

All right, I'm sorry for a question like this but I've searched long and hard for the error in my code and I'm stumped. I've been told the error is at line 145 and 71.

         public int whichQuestion;
     public int spaceBetweenQuestions = 100;
     
     private string [,] choices;
     private char [] answers;
     private string [] bonusQuestions;
     private bool [] wereQuestionsUsed;
     private bool popEnable = false;    
     private bool didValueChange = true;
     private bool? didAnswerCorrect = null;
     private char? letterPressed;
     private bool canAnswerQuestion = false;
     
     // Use this for initialization
     void Start () {
             
         int x;
         
         choices = new string [3, 4];
         bonusQuestions = new string [3];
         wereQuestionsUsed = new bool [3];
         answers = new char [3];
 
         letterPressed = null;
         
         for (x = 0; x < 3; x++) {
             
             wereQuestionsUsed [x] = false;
         }
         
         //Bonus questions that the player can answer for extra points.  This line also assigns lines for the questions and answers.
     }
     
     void OnGUI (){
         
         int x;
         
         if (popEnable){ // Error in the line below this
             GUI.Box (new Rect (Screen.width * 0.05f , Screen.height * 0.05f, Screen.width * 0.9f, Screen.height * 0.9f), 
                      "Bonus Trivia!\n" +
                      "Press the letter of the correct answer!\n" +
                      bonusQuestions [whichQuestion - 1] +
                      "\n");
             
             wereQuestionsUsed [whichQuestion - 1] = true;
             
             for (x = 0; x < 4; x++){
                 
                 GUI.Label (new Rect (Screen.width * 0.05f + 5, (Screen.height * 0.05f) + (spaceBetweenQuestions * (x + 4)),
                                      Screen.width * 0.9f - 10, 60),
                            choices [whichQuestion - 1, x]);
             }
             
             canAnswerQuestion = true;
         }
         
         if (didAnswerCorrect == true){
             
             popEnable = false;
             GUI.Box (new Rect (Screen.width *0.25f, Screen.height * 0.3f, Screen.width * 0.75f, Screen.height * 0.7f),
                      "Congratulations!\n" +
                      "You have answered correctly!");
             
             didAnswerCorrect = null;
         }
         else if (didAnswerCorrect == false){
             
             popEnable = false;
             GUI.Box (new Rect (Screen.width *0.25f, Screen.height * 0.3f, Screen.width * 0.75f, Screen.height * 0.7f),
                      "Sorry!\n" +
                      "You have answered incorrectly!" +
                      "Better luck next time!");
             
             didAnswerCorrect = null;
         }
     }
     
     void OnTriggerEnter (Collider otherObject){
         
         if (otherObject.tag == "Player"){
             
             popEnable = true;
             didValueChange = false;
         }
     }
     
     // Update is called once per frame
     void Update () {
         
         if (popEnable && !didValueChange){
             whichQuestion = (int) Random.Range (0.1f, 3.0f);
             didValueChange = true;
             
             if (canAnswerQuestion){
                 
                 if (Input.GetKeyDown ("a")){
                     letterPressed = 'a';
                     canAnswerQuestion = false;
                 }
                 else if (Input.GetKeyDown ("b")){
                     letterPressed = 'b';
                     canAnswerQuestion = false;
                 }
                 else if (Input.GetKeyDown ("c")){
                     letterPressed = 'c';
                     canAnswerQuestion = false;
                 }
                 else if (Input.GetKeyDown ("d")){
                     letterPressed = 'd';
                     canAnswerQuestion = false;
                 }
             }
             if (letterPressed == answers [whichQuestion - 1]) //Error in this line
                 didAnswerCorrect = true;
             else if (letterPressed != answers [whichQuestion - 1])
                 didAnswerCorrect = false;
         }
     }


I apologize if this code seems a little long as I included everything I thought might be needed. Any help or feedback is greatly appreciated. Thank you!

more ▼

asked Feb 04, 2012 at 04:44 AM

chaomein gravatar image

chaomein
1 3 2 3

What, exactly, is this line supposed to do?

 whichQuestion = (int) Random.Range (0.1f, 3.0f);

Why are you using the 'float' variant of Random.Range, instead of the 'int' version? In any case, there's nothing there that limits 'whichQuestion' to the length of your arrays, so you're naturally going to get indexoutofrange exceptions at some point.

Feb 04, 2012 at 04:46 AM syclamoth

hehe In case you haven't guessed, I'm pretty knew to this so when I stumbled upon Random.Range, I guess I only saw the float version. Thanks for the tip!

Feb 04, 2012 at 05:22 AM chaomein
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

In every line where you use

 someArray[whichQuestion - 1]

you should instead use

 someArray[whichQuestion]

and in this line

 whichQuestion = (int) Random.Range (0.1f, 3.0f);

you should use

 whichQuestion = Random.Range (0, answers.Length);

This should prevent the errors you are reporting.

more ▼

answered Feb 04, 2012 at 04:47 AM

syclamoth gravatar image

syclamoth
18.4k 20 28 118

Thank you so much! I will try this

Feb 04, 2012 at 05:22 AM chaomein
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x10207
x700

asked: Feb 04, 2012 at 04:44 AM

Seen: 2076 times

Last Updated: Feb 04, 2012 at 05:22 AM