Issues with timers and buttons

Hello.

Currently, I am on lecture 61 of the Unity 2D course. I’m having a few issues:

  • Firstly, the “Fill Fraction” is counting down from 2 rather than 1.
  • Secondly, when I click on a button, the game doesn’t do anything. The button won’t change colour and a message doesn’t come up showing the correct answer.

If it’s of any usefulness, below is the code from the scripts I believe are triggering the issue.

Thanks.

Sam.

Timer.CS

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Timer : MonoBehaviour
{
    [SerializeField] float timeToCompleteQuestion = 15;
    [SerializeField] float timeToShowCorrectAnswer = 8f;

    public bool loadNextQuestion;
    public float fillFraction;

    bool isAnsweringQuestion;
    float timerValue;

    // Update is called once per frame
    void Update()
    {
        UpdateTimer();   
    }
    public void CancelTimer()
    {
        timerValue = 0;
    }

    void UpdateTimer()
    {
        timerValue -= Time.deltaTime;
        if (isAnsweringQuestion)
        {
            if (timerValue > 0)
            {
                fillFraction = timerValue / timeToCompleteQuestion;
            }
            else
            {
                isAnsweringQuestion = false;
                timerValue = timeToShowCorrectAnswer;
            }
        }
        else
        {
            if (timerValue > 0)
            {
                fillFraction = timerValue / timeToShowCorrectAnswer;
            }
            else
            {
                isAnsweringQuestion = false;
                timerValue = timeToCompleteQuestion;
                loadNextQuestion = true;
            }
        }
    }
}

Quiz.CS

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

public class Quiz : MonoBehaviour
{
[Header(“Questions”)]
[SerializeField] TextMeshProUGUI questionText;
[SerializeField] QuestionSO question;

[Header("Answers")]
[SerializeField] GameObject[] answerButtons;
int correctAnswerIndex;

[Header("Buttons")]
[SerializeField] Sprite defaultAnswerSprite;
[SerializeField] Sprite correctAnswerSprite;

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

// Start is called before the first frame update
void Start()
{
    timer = FindObjectOfType<Timer>();
    GetNextQuestion();
    // DisplayQuestion();
}

void Update()
{
    timerImage.fillAmount = timer.fillFraction;
    if (timer.loadNextQuestion)
    {
        GetNextQuestion();
        timer.loadNextQuestion = false;
    }
}

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);

    }

}
public void OnAnswerSelected(int index)
{
    Image buttonImage;

    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;
    }
    SetButtonState(false);
    timer.CancelTimer();
}
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++)
    {
        Image buttonImage;
        buttonImage = answerButtons[i].GetComponent<Image>();
        buttonImage.sprite = defaultAnswerSprite;
    }

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

}

Hi Sam,

Which version of Unity do you use?

  1. Could you share a screenshot of your Timer component? If the value of fillFraction is 2 and you expected it to be 1, the issue might be there. However, the value of fillFraction also gets set in UpdateTimer, so it is also possible that the issue is caused there. Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

  2. Do the OnClick fields of the buttons reference a method in your code?


See also:

Privacy & Terms