Answering Question is not Switching Timer state

issue with my code timer not switching states. from the answering question to question answered.

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

public class Quiz : MonoBehaviour
{   
    [Header("Questions")]
    [SerializeField] TextMeshProUGUI questionText;
    [SerializeField] List<QuestionIO> questions = new List<QuestionIO>();
    QuestionIO currentQuestion;

    [Header("Answers")]
    [SerializeField] GameObject[] answerButtons;
    int correctAnswerIndex;
    bool hasAnsweredEarly;
    
    [Header("Buttons")]
    [SerializeField] Sprite defaultAnswerSprite;
    [SerializeField] Sprite correctAnswerSprite;

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

    [Header("Scoring")]
    [SerializeField] TextMeshProUGUI scoreText;
    ScoreKeeper scorekeeper;

    [Header("Progress Bar")]
    [SerializeField] Slider progressBar;


    public bool isComplete;

    void Start()
    {
        timer = FindObjectOfType<Timer>();
        scorekeeper = FindObjectOfType<ScoreKeeper>();
        progressBar.maxValue = questions.Count;
        progressBar.value = 0;
    }

    void Update()
    {
        timerImage.fillAmount = timer.fillfraction;
        if(timer.loadNextQuestion)
        {
            hasAnsweredEarly = false;
            GetNextQuestion();
            timer.loadNextQuestion = false;
        }
        else if(!hasAnsweredEarly && !timer.isAnsweringQuestion)
        {
            //by passing in a -1 because the index is 0 to 3 -1 will always give a false answer
            DisplayAnswer(-1);
            SetButtonState(false);
        }
    }

    public void OnAnswerSelected(int index)  

    {
        hasAnsweredEarly = true;
        DisplayAnswer(index);
        SetButtonState(false);
        timer.CancelTimer();
        scoreText.text = "Score: " + scorekeeper.CalculateScore() + "%"; 

        if(progressBar.value == progressBar.maxValue)
        {
            isComplete = true;
        }
    }

    void DisplayAnswer(int index)
    {
        Image buttonImage;

        if(index == currentQuestion.GetCorrectAnswerIndex())
        {
            questionText.text = "correct";
            buttonImage = answerButtons[index].GetComponent<Image>();
            buttonImage.sprite = correctAnswerSprite;
            scorekeeper.IncrementCorrectAnswers();
        }
        else
        {
            correctAnswerIndex = currentQuestion.GetCorrectAnswerIndex();
            string correctAnswer = currentQuestion.GetAnswer(correctAnswerIndex);
            questionText.text = "Sorry the correct answer is \n" + correctAnswer;

            buttonImage = answerButtons[correctAnswerIndex].GetComponent<Image>();
            buttonImage.sprite = correctAnswerSprite;
        }
    }
    
    void GetNextQuestion()
    {
        if(questions.Count > 0)
        {
        SetButtonState(true);
        SetDefaultButtonSprit();
        GetRandomQuestion();
        DisplayQuestion();
        progressBar.value++;
        scorekeeper.IncrementQuestionsSeen();
        }

    }

    void GetRandomQuestion()
    {
        int index = Random.Range(0, questions.Count);
        currentQuestion = questions[index];
        if(questions.Contains(currentQuestion))
        {
            questions.Remove(currentQuestion);
        }
    }

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

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

    void SetButtonState(bool state)
    {
        for(int i = 0; i < answerButtons.Length; i++)
        {
            Button button = answerButtons[i].GetComponent<Button>();
            button.interactable = state;
        }
    }
    void SetDefaultButtonSprit()
    {
        for(int i = 0; i < answerButtons.Length; i++)
        {
            Image buttonImage = answerButtons[i].GetComponent<Image>();
            buttonImage.sprite = defaultAnswerSprite;
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Timer : MonoBehaviour
{
    [SerializeField] float timeToCompletQuestion = 30f;
    [SerializeField] float timeToShowCorrectAnswer = 6f;

    public bool loadNextQuestion;

    public bool isAnsweringQuestion = false;
    public float fillfraction;

    float timerValue;

    void Update()
    {
        UpdateTimer();
    }

    public void CancelTimer()
    {
        timerValue = 0;
    }

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

        if(isAnsweringQuestion)
        {
            if(timerValue > 0)
            {
                fillfraction = timerValue / timeToCompletQuestion; 
            }
            if(timerValue <= 0)
            {
                isAnsweringQuestion = false;
                timerValue = timeToShowCorrectAnswer;
            }
        }
        else
        {
            if(timerValue > 0)
            {
                fillfraction = timerValue / timeToShowCorrectAnswer; 
            }
            else
            {
                isAnsweringQuestion = true;
                timerValue = timeToCompletQuestion;
                loadNextQuestion = true;
            }
        }
    }
}

Hi,

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

yes but i cant see where i went wrong

I ran the code you posted (in a copy that works) and it was moving from question to question as expected, but the last (?) question did not display the answer, it just went straight to the ‘complete’ screen. The reason for that is because this code here

        if(progressBar.value == progressBar.maxValue)
        {
            isComplete = true;
        }

is in the wrong place. When I moved it to where it should be

    void Update()
    {
        timerImage.fillAmount = timer.fillfraction;
        if(timer.loadNextQuestion)
        {
            // It should be here 
            if(progressBar.value == progressBar.maxValue)
            {
                isComplete = true;
                return; // <- this should also be added
            }
            hasAnsweredEarly = false;
            GetNextQuestion();
            timer.loadNextQuestion = false;
        }
        else if(!hasAnsweredEarly && !timer.isAnsweringQuestion)
        {
            //by passing in a -1 because the index is 0 to 3 -1 will always give a false answer
            DisplayAnswer(-1);
            SetButtonState(false);
        }
    }

It displayed the answer before completing the game.

I have a ? next to last because I don’t think it was the last question. I am checking it now
EDIT I turns out it’s the same issue mentioned in this question

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

Privacy & Terms