Problem with GetQuestion

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;
        }
    }

    
}

Hi,

did you drag everything in the serializefields?

Yeah, I did :thinking:

Hi Kocian,

Welcome to our community! :slight_smile:

NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. That’ll help you analyse the problem further.


See also:

Problem is on line 115 and 43 but I don’t know what is wrong. If I made timer by way of Garry, there wouldn’t be a problem probably, but I made the timer by my own way and I’m proud of it, but after this lecture it doesn’t work. it works, if I insert Displayquestions() or line 115 to the comment

here are the lines which are problem.
questiontext.text = currentquestion.GetQuestion();
OnAnswerSelected(currentquestion.GetCorrectAnswerIndex());

Thank you. In these two lines, there are two variables that might be null: questiontext and currentquestion. Check where the objects get assigned to the varaibles. And make sure that the lines do not get executed before you assign an object.

I had made huge chaos, so I copied the script from Gary. Nevermind. At least it works now.
Anyway, thanks for trying to help me

You’re welcome. I’m glad your game works now. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms