2d game Video 63 index is outside the bound of array

Video 63 , its showing error when timer runs out: index was outside the bounds of array

Hi,

Welcome to our community! :slight_smile:

ArgumentOutOfRange / IndexOutOfRange exception means that the code tried to access a non-existent index in an array, List or another collection. The first index in an array and List is 0, hence the last index is the length minus 1. For example, if the array has got a length of 2, and the code tries to access index 2, you will get this error message because there are only two “slots” in the array: at index 0 and at index 1.

Did this help?


See also:

Can you please find error , without this i cant continue my course:

`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;

public class Quiz : MonoBehaviour
{
[Header(“Questions”)]
[SerializeField] TextMeshProUGUI questionDisplay;
[SerializeField] questions questionData;

[Header("Answers")]
[SerializeField] GameObject[] ansButtons;
int correctAnswerIndex;
bool hasAnsweredEarly;

[Header("Button Colours")]
[SerializeField] Sprite defaultAnswerSprite;
[SerializeField] Sprite correctAnswerSprite;
[SerializeField] Sprite wrongAnswerSprite;
[SerializeField] Sprite wrongCorrectSprite;

[Header("Timer")]
[SerializeField] Image timerImage;
 timer timer;


void Start()
{
    timer = FindObjectOfType<timer>();
    //DisplayQuestion();
    GetNextQuestion();
}

void Update()
{
    timerImage.fillAmount = timer.fillFraction;
    
          if(timer.showNextQuestion)
          {
            hasAnsweredEarly = false;
            GetNextQuestion();
            timer.showNextQuestion = false;
          }
          else if (!hasAnsweredEarly && !timer.isAnsweringQuestion)
          {
                DisplayAnswer(-1);
                SetButtonState(false);
          }
    
}

public void OnAnswerSelected(int index)
{

    hasAnsweredEarly = true;
    DisplayAnswer(index);
    SetButtonState(false);
    timer.CancelTimer();

}

void DisplayAnswer(int index)
{
Image buttonImage;

 if (index == questionData.GetCorrectAnswerIndex())
 {
    questionDisplay.text = "That's Right!";
    buttonImage = ansButtons[index].GetComponent<Image>();
    buttonImage.sprite = correctAnswerSprite;
}
else 
{
    correctAnswerIndex = questionData.GetCorrectAnswerIndex();
    string correctAnswer = questionData.GetAnswers(correctAnswerIndex);
    questionDisplay.text = "No! you are so wrong lol :D; \n" + correctAnswer + " Was correct XD...";
    buttonImage = ansButtons[index].GetComponent<Image>();
    buttonImage.sprite = wrongAnswerSprite;
    buttonImage = ansButtons[correctAnswerIndex].GetComponent<Image>();
    buttonImage.sprite = wrongCorrectSprite;
    }

}

void GetNextQuestion()
{
    SetButtonState(true);
    SetDefaultButtonSprites();
    DisplayQuestion();
}

void DisplayQuestion()
{
questionDisplay.text = questionData.GetQuestion();

    for(int i = 0; i < ansButtons.Length; i ++)
    {
    TextMeshProUGUI buttonText = ansButtons[i].GetComponentInChildren<TextMeshProUGUI>();
    buttonText.text = questionData.GetAnswers(i);
    }

}

void SetButtonState(bool stat)
{
for (int i = 0; i < ansButtons.Length; i++)
{
Button button = ansButtons[i].GetComponent();
button.interactable = stat;
}
}

void SetDefaultButtonSprites()
{
Image defaultImage;

for (int i = 0; i < ansButtons.Length; i++)

{
    defaultImage = ansButtons[i].GetComponent<Image>();
    defaultImage.sprite = defaultAnswerSprite;
}

}

}
`

Probably here. You are passing -1 if the timer ran out without an answer being picked. -1 is not a valid index but you are trying to find ansButtons[index]

how to fix it? if timer runs out then it should show the correct answer im beginner and it is very complicated for me. i just want to finish it asap

You’re trying to change an image on a button, but no button was clicked. If the index is -1, don’t change the image

void DisplayAnswer(int index)
{
    Image buttonImage;
    if (index == questionData.GetCorrectAnswerIndex())
     {
        questionDisplay.text = "That's Right!";
        buttonImage = ansButtons[index].GetComponent<Image>();
        buttonImage.sprite = correctAnswerSprite;
    }
    else 
    {
        correctAnswerIndex = questionData.GetCorrectAnswerIndex();
        string correctAnswer = questionData.GetAnswers(correctAnswerIndex);
        questionDisplay.text = "No! you are so wrong lol :D; \n" + correctAnswer + " Was correct XD...";
        // Only change the image if the player selected the wrong answer (index is not -1)
        if (index != -1)
        {
            buttonImage = ansButtons[index].GetComponent<Image>();
            buttonImage.sprite = wrongAnswerSprite;
        }
        buttonImage = ansButtons[correctAnswerIndex].GetComponent<Image>();
        buttonImage.sprite = wrongCorrectSprite;
    }
}
1 Like

Thank you very much! Solved!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms