using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class Quiz : MonoBehaviour
{
[Header("Questions")]
[SerializeField] TextMeshProUGUI quesText;
[SerializeField]List<Question> questions = new List<Question>();
Question currentQuestion;
[Header("Answers")]
[SerializeField] GameObject[] ansButtons;
int correctAnswerIndex;
bool hasAnsEarly;
[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.loadNextQues)
{
hasAnsEarly = false;
GetNextQues();
timer.loadNextQues = false;
}
else if (!hasAnsEarly && !timer.isAnswering)
{
DisplayAnswer(-1);
ButtonState(false);
}
}
public void OnAnswerSelected(int index)
{
hasAnsEarly = true;
DisplayAnswer(index);
ButtonState(false);
timer.CancelTimer();
}
void DisplayAnswer(int index)
{
Image buttonImage;
if(index == currentQuestion.GetCorrectAnswerIndex())
{
quesText.text = "Correct!";
buttonImage = ansButtons[index].GetComponent<Image>();
buttonImage.sprite = correctAnswerSprite;
}
else if(index == -1)
{
quesText.text = "Correct Answer is " + currentQuestion.GetAnswer(currentQuestion.GetCorrectAnswerIndex());
buttonImage = ansButtons[currentQuestion.GetCorrectAnswerIndex()].GetComponent<Image>();
buttonImage.sprite = correctAnswerSprite;
}
else{
quesText.text = "Wrong answer!\nCorrect Answer is " + currentQuestion.GetAnswer(currentQuestion.GetCorrectAnswerIndex());
buttonImage = ansButtons[currentQuestion.GetCorrectAnswerIndex()].GetComponent<Image>();
buttonImage.sprite = correctAnswerSprite;
}
}
void GetNextQues()
{
if(questions.Count > 0)
{
ButtonState(true);
SetDefaultButtonSprites();
GetCurrentQuestion();
DisplayQuestion();
}
}
void GetCurrentQuestion()
{
int qIndex = Random.Range(0, questions.Count);
currentQuestion = questions[qIndex];
Debug.Log("Current qestion"+ currentQuestion);
if (questions.Contains(currentQuestion))
{
questions.Remove(currentQuestion);
}
}
void DisplayQuestion()
{
quesText.text = currentQuestion.GetQuestion();
for(int i = 0; i < ansButtons.Length; i++)
{
TextMeshProUGUI buttonText = ansButtons[i].GetComponentInChildren<TextMeshProUGUI>();
buttonText.text = currentQuestion.GetAnswer(i);
}
}
void ButtonState(bool state)
{
for (int i = 0; i< ansButtons.Length; i++ )
{
Button button = ansButtons[i].GetComponent<Button>();
button.interactable = state;
}
}
void SetDefaultButtonSprites()
{
for (int i = 0; i< ansButtons.Length; i++ )
{
Image buttonImage = ansButtons[i].GetComponent<Image>();
buttonImage.sprite = defaultAnswerSprite;
}
}
}
This is the Quiz script and its almost same as the one that Gary used. When I start the game only one question, the question that was displayed, was supposed to be deleted. But instead 2 questions are being deleted. This is after removing the GetNextQues() method call, where only 1 question was to deleted. When I called the method in Start(), 3 questionsare being removed. Can anyone help me with it? Copying Gary’s script didn’t help either.