Have some trouble on showing correct answer if not answering

When I let the timer run out. My button still interactable and not changing the sprite of correct answer.
i don’t know what go wrong in my code but there some part I change to my liking.
I try copy the whole code but it still happening.
here’s my code.

public class Quiz : MonoBehaviour
{
    [Header("Question")]
    [SerializeField] TextMeshProUGUI questionText;
    [SerializeField] QuestionSO question;

    [Header("Answer")]
    [SerializeField] GameObject[] answerButton;
    int correctAnswerIndex;
    bool hasAnswerEarly;

    [Header("Button")]
    [SerializeField] Sprite defaultAnswerSprite;
    [SerializeField] Sprite correctAnswerSprite;
    [SerializeField] Sprite wrongAnswerSprite;
    Image buttonImage;

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

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

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



    public void OnAnswerSelected(int index)
    {
        hasAnswerEarly = true;
        DisplayAnswer(index);

        SetButtonState(false);
        timer.CancelTimer();
    }

    void DisplayAnswer(int index)
    {
        correctAnswerIndex = question.GetCorrectAnswer();
        if (index == correctAnswerIndex)
        {
            questionText.text = "Correct";
        }
        else
        {
            string correctAnswer = question.GetAnswer(correctAnswerIndex);
            questionText.text = "WRONG, the answer is; \n" + correctAnswer;
            buttonImage = answerButton[index].GetComponent<Image>();
            buttonImage.sprite = wrongAnswerSprite;
        }
        buttonImage = answerButton[correctAnswerIndex].GetComponent<Image>();
        buttonImage.sprite = correctAnswerSprite;
        SetButtonState(false);
    }

    void DisplayQuestion()
    {
        questionText.text = question.GetQuestion();

        for (int i = 0; i < answerButton.Length; i++)
        {
            TextMeshProUGUI buttonText = answerButton[i].GetComponentInChildren<TextMeshProUGUI>();
            buttonText.text = question.GetAnswer(i);
        }
        
    }

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

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

    void SetButtonSprite()
    {
        for (int i = 0; i < answerButton.Length; i++)
        {
            buttonImage = answerButton[i].GetComponent<Image>();
            buttonImage.sprite = defaultAnswerSprite;
        }
    }
}
public class Timer : MonoBehaviour
{
    public bool isAnsweringQuestion;
    public float fillFraction;
    public bool loadNextQuestion;
    
    float timerValue;
    [SerializeField] float timeToAnswer = 30f;
    [SerializeField] float timeToCorrectAnswer = 10f;

    void Start()
    {
        timerValue = timeToAnswer;
    }
    void Update()
    {
        UpdateTimer();
    }
    public void CancelTimer()
    {
        timerValue = 0;
    }

    void UpdateTimer()
    {
        timerValue -= Time.deltaTime;

        if (isAnsweringQuestion)
        {
            if(timerValue > 0)
            {
                fillFraction = timerValue / timeToAnswer;
            }
            else
            {
                isAnsweringQuestion = false;
                timerValue = timeToCorrectAnswer;
            }
        }
        else
        {
            if (timerValue > 0)
            {
                fillFraction = timerValue / timeToCorrectAnswer;
            }
            else
            {
                isAnsweringQuestion = true;
                timerValue = timeToAnswer;
                loadNextQuestion = true;
            }
        }

        Debug.Log(isAnsweringQuestion + " : " + timerValue + " = " + fillFraction);
    }

    
}

From [Complete C# Unity Game Developer 2D] part 63

Hi,

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? And have you already tried to add Debug.Logs to your code to see what is going on during runtime?

Privacy & Terms