Hello, I am on lesson 63 and I am having trouble with my code. when every I interact with my game
it always replies with me getting the wrong answer. I believe that there is something wrong with the code but I don’t know.
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;
}
}
}
}
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 correctAnswerIndext;
bool hasAnsweredEurly;
[Header("Button Colors")]
[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)
{
hasAnsweredEurly = false;
GetNextQuestion();
timer.loadNextQuestion = false;
}
else if(!hasAnsweredEurly && timer.isAnsweringQuestion)
{
DisplayAnswer(-1);
SetButtonState(false);
}
}
public void OnAnswerSelected(int index)
{
hasAnsweredEurly = true;
DisplayAnswer(index);
SetButtonState(false);
timer.CancelTimer();
}
void DisplayAnswer(int index)
{
Image buttonImage;
if (index == currentQuestion.GetCorrectAnswerIndex())
{
questionText.text = "Correct!";
buttonImage = answerButtons[index].GetComponent<Image>();
buttonImage.sprite = correctAnswerSprite;
}
else
{
correctAnswerIndext = currentQuestion.GetCorrectAnswerIndex();
string correctAnswer =currentQuestion.Getanswers(correctAnswerIndext);
questionText.text ="Sorry, the correct answer was;\n" +correctAnswer;
buttonImage =answerButtons[correctAnswerIndext].GetComponent<Image>();
buttonImage.sprite = correctAnswerSprite;
}
}
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.Getanswers(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;
}
}
}