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