Hello.
I made timer by myself and it has worked. But… When I modified the code in this lecture, it shows errors:
NullReferenceException: Object reference not set to an instance of an object Quiz.Display.Question()
NullReferenceException: Object reference not set to an instance of an object: Quiz.Timer()
If I put Display.Questions() which is in GetNextQuestion to the comment, it works, but it doesn’t show the question.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
// using System;
public class Quiz : MonoBehaviour
{
[Header("test")]
[SerializeField]TextMeshProUGUI questiontext;
[SerializeField] List<QuestionSO> questions = new List<QuestionSO>();
QuestionSO currentquestion;
[SerializeField] GameObject[] Answerbuttons;
[SerializeField] Image timerimg;
bool timeron = true;
// int GetCorrectAnswerIndex;
[SerializeField] Sprite DefaultSprite;
[SerializeField] Sprite CorrectSprite;
int correct;
void Start()
{
GetNextQuestion();
}
void Update()
{
Timer();
}
void Timer()
{
timerimg.GetComponent<Image>();
if(timeron)
{
timerimg.fillAmount -= Time.deltaTime * 0.08f;
}
if(timerimg.fillAmount == 0)
{
OnAnswerSelected(currentquestion.GetCorrectAnswerIndex());
}
}
public void OnAnswerSelected(int index)
{
if(index == currentquestion.GetCorrectAnswerIndex() && timerimg.fillAmount != 0)
{
questiontext.text = "Spravne";
Image ButtonImage = Answerbuttons[index].GetComponent<Image>();
ButtonImage.sprite = CorrectSprite;
}
else if(timerimg.fillAmount <= 0)
{
correct = currentquestion.GetCorrectAnswerIndex();
string correctansser = currentquestion.GetAnswer(correct);
questiontext.text = "Cas vyprsel. Spravna odpoved: " + correctansser;
Image ButtonImage = Answerbuttons[currentquestion.GetCorrectAnswerIndex()].GetComponent<Image>();
ButtonImage.sprite = CorrectSprite;
}
else
{
/* questiontext.text = "Spatne";
Image ButtonImage = Answerbuttons[correct].GetComponent<Image>();
ButtonImage.sprite = CorrectSprite;*/
correct = currentquestion.GetCorrectAnswerIndex();
string correctansser = currentquestion.GetAnswer(correct);
questiontext.text = "Mas to blbe. Spravne to je: " + correctansser;
Image ButtonImage = Answerbuttons[currentquestion.GetCorrectAnswerIndex()].GetComponent<Image>();
ButtonImage.sprite = CorrectSprite;
}
Invoke("GetNextQuestion", 2f);
SetButtonState(false);
timeron = false;
}
void SetDefaultSprite()
{
for(int i = 0; i < Answerbuttons.Length; i++)
{
Image ButtonImage = Answerbuttons[i].GetComponent<Image>();
ButtonImage.sprite = DefaultSprite;
}
}
void GetNextQuestion()
{
SetButtonState(true);
DisplayQuestion();
SetDefaultSprite();
GetRandomQuestion();
timeron = true;
timerimg.fillAmount = 1f;
}
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);
}
correct = currentquestion.GetCorrectAnswerIndex();
}
void SetButtonState(bool state)
{
for(int i = 0; i < Answerbuttons.Length; i++)
{
Button button = Answerbuttons[i].GetComponent<Button>();
button.interactable = state;
}
}
}