Hi
So I am on the Quizmaster course and its getting quite complex but im enjoying it! Something strange is happening with my code, in that every time I select an answer a question is removed, then another question is removed when the correct answer is shown. So it cuts my question list in half. If I let the timer run out on its own, all the questions are shown instead.
What is going on here?
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] List<QuestionSO> questions = new List<QuestionSO>();
QuestionSO 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;
void Start()
{
timer = FindObjectOfType<Timer>();
}
void Update()
{
timerImage.fillAmount = timer.fillFraction;
if (timer.loadNextQuestion)
{
hasAnsweredEarly = false;
GetNextQuestion();
timer.loadNextQuestion = false;
}
else if (!hasAnsweredEarly && !timer.isAnsweringQuestion)
{
DisplayAnswer(-1);
SetButtonState(false);
}
}
void DisplayAnswer(int index)
{
Image buttonImage;
if (index == currentQuestion.GetCorrectAnswerIndex())
{
questionText.text = "Correct!";
buttonImage = answerButtons[index].GetComponent<Image>();
buttonImage.sprite = correctAnswerSprite;
}
else
{
correctAnswerIndex = currentQuestion.GetCorrectAnswerIndex();
string correctAnswer = currentQuestion.GetAnswer(correctAnswerIndex);
questionText.text = "Nope! The correct answer was:\n" + correctAnswer;
buttonImage = answerButtons[correctAnswerIndex].GetComponent<Image>();
buttonImage.sprite = correctAnswerSprite;
}
}
public void OnAnswerSelected(int index)
{
hasAnsweredEarly = true;
DisplayAnswer(index);
SetButtonState(false);
GetRandomQuestion();
timer.CancelTimer();
}
void GetNextQuestion()
{
if (questions.Count > 0)
{
SetButtonState(true);
SetDefaultButtonSprites();
GetRandomQuestion();
DisplayQuestion();
}
}
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 SetDefaultButtonSprites()
{
for (int i = 0; i < answerButtons.Length; i++)
{
Image buttonImage = answerButtons[i].GetComponent<Image>();
buttonImage.sprite = defaultAnswerSprite;
}
}
}
Timer.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Timer : MonoBehaviour
{
[SerializeField] float timeToCompleteQuestion = 30f;
[SerializeField] float timeToShowCorrectAnswer = 10f;
public bool loadNextQuestion;
public float fillFraction;
public bool isAnsweringQuestion;
float timerValue;
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 = true;
timerValue = timeToCompleteQuestion;
loadNextQuestion = true;
}
}
}
}
QuestionSO.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(menuName = "Quiz Question", fileName = "New Question")]
public class QuestionSO : ScriptableObject
{
[TextArea(2, 6)]
[SerializeField] string question = "Enter new question text here";
[SerializeField] string[] answers = new string[4];
[SerializeField] int correctAnswerIndex;
public string GetQuestion()
{
return question;
}
public string GetAnswer(int index)
{
return answers[index];
}
public int GetCorrectAnswerIndex()
{
return correctAnswerIndex;
}
}