Problem with the index

So, I have a one problem with the index, when I trying to answer the question. Choosing every answer on question, even if I have a correct answer, It takes me -1 index a lot of time. I added debug to show what is the problem.

The code is

void Start()

{

    timer = FindAnyObjectByType<Timer>();

    GetNextQuestion();

    //DisplayQuestion();

}

void Update()

{

    timerImage.fillAmount = timer.fillFraction;

    if (timer.loadNextQuestion)

    {

        hasAnsweredEarly = false;

        GetNextQuestion();

        timer.loadNextQuestion = 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)

{

    if (index == question.GetCorrectAnswerIndex())

    {

        questionText.text = "Correct!";

        buttonImage = answerButtons[index].GetComponent<Image>();

        buttonImage.sprite = correctAnswerSprite;

    }

    else

    {

        correctAnswerIndex = question.GetCorrectAnswerIndex();

        string correctAnswer = question.GetAnswer(correctAnswerIndex);

        questionText.text = "Sorry, the correct answer was;\n" + correctAnswer;

        buttonImage = answerButtons[correctAnswerIndex].GetComponent<Image>();

        buttonImage.sprite = correctAnswerSprite;

    }

    Debug.Log(index);

}

void GetNextQuestion()

{

    SetButtonState(true);

    SetDefaultButtonSprites();

    DisplayQuestion();

}

void DisplayQuestion()

{

    questionText.text = question.GetQuestion();

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

    {

        TextMeshProUGUI buttonText = answerButtons[i].GetComponentInChildren<TextMeshProUGUI>();

        buttonText.text = question.GetAnswer(i);

    }

}

void SetButtonState(bool state)

{

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

    {

        Button button = answerButtons[i].GetComponent<Button>();

        button.interactable = state;

    }

}

void SetDefaultButtonSprites()

{

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

    {

        buttonImage = answerButtons[i].GetComponent<Image>();

        buttonImage.sprite = defaultAnswerSprite;

    }

}

From the console messages it looks like your timer isn’t stopping or not changing state properly. You can see the button’s index making it to the DisplayAnswer() but then, a few seconds later, -1 is sent to DisplayAnswer(). You can also see it’s being sent repeatedly which means it’s coming from Update()

Check that the timer’s state is being set correctly when an answer is selected by the player

Ok, I checked the code from Timer.cs.
I didn’t find some suspicious and it looks correctly, maybe you can find any problem from this code.
public class Timer : MonoBehaviour

{

[SerializeField] float timeToCompleteQuestion = 30f;

[SerializeField] float timeToShowCorrectAnswer = 10f;

public bool loadNextQuestion;

public bool isAnsweringQuestion;

public float fillFraction;

float timerValue;

void Update()

{

    UpdateTimer();

}

public void CancelTimer()

{

    timerValue = 0;

}

void UpdateTimer()

{

    timerValue -= Time.deltaTime;

    timerValue = timerValue - Time.deltaTime;

    if (isAnsweringQuestion)

    {

        if (timerValue > 0)

        {

            fillFraction = timerValue / timeToCompleteQuestion;

        }

        else

        {

            isAnsweringQuestion = false;

            timerValue = timeToShowCorrectAnswer;

            loadNextQuestion = true;

        }

    }

    else

    {

        if (timerValue > 0)

        {

            fillFraction = timerValue / timeToShowCorrectAnswer;

        }

        else

        {

            isAnsweringQuestion = true;

            timerValue = timeToCompleteQuestion;

            loadNextQuestion = true;

        }

    }

}

}

Hi Kayo,

How did you check the code? There are lots of variables in it whose values are known at runtime only. Have you already tried to add Debug.Logs to your code to see what is going on while your game is running?

First a note: you’re subtracting deltaTime from the timer twice, so your timer is going to be twice as fast as you expect.

Now, @Nina is right; you’ll need to check the values of all those variables at runtime to track down what is happening. From the code, it appears there is a time when hasAnsweredEarly and timer.isAnsweringQuestion is true at the same time - which shouldn’t happen. Try to determine where that is and why it is happening

Privacy & Terms