Hello,
When I try running my game, the questions are coming up as incorrect on 7/8 of them. I have checked in the inspector that I have the correct element selected as the answer. I have also tried selecting the other 3 incorrect answers to see if they would register as the correct answer for some reason, however; they do not. I am not really certain on the code error as I think it all looks similar to the instructors. Any assistance would be greatly appreciated.
Below is my my quiz script as reference:
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 questions = new List ();
QuestionSO currentQuestion;
[Header(“Answers”)]
bool hasAnsweredEarly;
[SerializeField] GameObject answerButtons;
int correctAnswerIndex;
[Header(“Button Colors”)]
[SerializeField] Sprite defaultAnswerSprite;
[SerializeField] Sprite correctAnswerSprite;
[Header(“Timer”)]
[SerializeField] Image timerImage;
Timer timer;
[Header (“Scoring”)]
[SerializeField] TextMeshProUGUI scoreText;
ScoreKeeper scoreKeeper;
void Start()
{
timer = FindObjectOfType();
scoreKeeper = FindObjectOfType();
}
void Update()
{
timerImage.fillAmount = timer.fillFraction;
if (timer.LoadNextQuestion)
{
hasAnsweredEarly = false;
GetNextQuestion();
timer.LoadNextQuestion = false;
}
else if (!hasAnsweredEarly && !timer.isAnsweringQuestion)
{
DisplayAnswer(-1);
SetButtonState (false);
}
}
public void OnAnswerSelected(int index)
{
hasAnsweredEarly = true;
DisplayAnswer (index);
SetButtonState(false);
timer.CancelTimer();
scoreText.text = "Score: " + scoreKeeper.CalculateScore() + “%”;
}
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 was; \n” + correctAnswer;
buttonImage = answerButtons [correctAnswerIndex]. GetComponent();
buttonImage.sprite = correctAnswerSprite;
}
}
void GetNextQuestion()
{
if (questions.Count > 0)
{
SetButtonState(true);
SetDefaultButtonSprites();
GetRandomQuestion();
DisplayQuestion();
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 SetDefaultButtonSprites()
{
for (int i = 0; i < answerButtons.Length; i++)
{
Image buttonImage = answerButtons[i].GetComponent<Image>();
buttonImage.sprite = defaultAnswerSprite;
}
}
}