Hi there,
I am doing Quiz Game from the Unity 2d course and got into a problem that I cannot solve.
Here is the error:
NullReferenceException: Object reference not set to an instance of an object
Quiz.DisplayAnswers (System.Int32 index) (at Assets/Scripts/Quiz.cs:67)
Quiz.Update () (at Assets/Scripts/Quiz.cs:45)
And here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using System.Xml.Serialization;
public class Quiz : MonoBehaviour
{
[Header("Questions")]
[SerializeField] TextMeshProUGUI questionText;
[SerializeField] List<QuestionsSO> questions = new List<QuestionsSO>();
QuestionsSO currentQuestion;
[Header("Answers")]
[SerializeField] GameObject[] answerButtons;
int correctAnswerIndex;
bool hasAnsweredEarly;
[Header("Button colors")]
[SerializeField] Sprite defaultAnswerSprite;
[SerializeField] Sprite correctAnswerSprite;
Image buttonImage;
[Header("Timer")]
[SerializeField] Image timerImage;
Timer timer;
// Start is called before the first frame update
void Start()
{
timer = FindObjectOfType<Timer>();
}
private void Update()
{
timerImage.fillAmount = timer.fillFraction;
if (timer.loadNewQuestion)
{
hasAnsweredEarly = false;
GetNewQuestion();
timer.loadNewQuestion = false;
}
else if(!hasAnsweredEarly && !timer.isAnsweringQuestion)
{
DisplayAnswers(-1);
SetButtonState(false);
}
}
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);
}
}
public void OnAnswerSelection(int index)
{
hasAnsweredEarly = true;
DisplayAnswers(index);
SetButtonState(false);
timer.CancelTimer();
}
void DisplayAnswers(int index)
{
if (index == currentQuestion.GetCorrectAnswerIndex())
{
questionText.text = "Good!";
buttonImage = answerButtons[index].GetComponent<Image>();
buttonImage.sprite = correctAnswerSprite;
}
else
{
correctAnswerIndex = currentQuestion.GetCorrectAnswerIndex();
questionText.text = "Bad, the correct answer is: \n " + currentQuestion.GetAnswer(correctAnswerIndex);
buttonImage = answerButtons[correctAnswerIndex].GetComponent<Image>();
buttonImage.sprite = correctAnswerSprite;
}
}
void GetNewQuestion()
{
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 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++)
{
buttonImage = answerButtons[i].GetComponent<Image>();
buttonImage.sprite = defaultAnswerSprite;
}
}
}
EDIT 27.10.2024
**So the actual solution I found there:
https://community.gamedev.tv/t/nullreferenceexception/218345?_gl=1*8bruk2*_ga*MTkxMjYzMzI5LjE2ODI1NDUxMjE.*_ga_2C81L26GR9*MTY4MzY1NDgyOS4yOC4xLjE2ODM2NTUwMzYuMC4wLjA.
I had to leave loadNewQuestion as null as in the course and with that I got rid of this bug and also only 1 question is being removed from the list at the beginning of the game.
Cheers