Fixed null exception but do not understand why

I was getting a null exception query that was sending me to my DisplayQuestion() method and in particular the line highlighted:

void DisplayQuestion()
{
      questionText.text = currentQuestion.GetQuestion(); // error pointed here
   

    for (int i = 0; i < answerButtons.Length; i++)
    {
        TextMeshProUGUI buttonText = answerButtons[i].GetComponentInChildren<TextMeshProUGUI>();
        buttonText.text = currentQuestion.GetAnswer(i);
    }

}

On another thread here Alerazius posted a fix whereby one posts :slight_smile:

currentQuestion = ScriptableObject.CreateInstance<QuestionSO>();

into the Start() method. For me, this solved the problem. But I do not understand why.

Could someone please explain why, in this particular context, this is necessary?

Thanks,

K

1 Like

It shouldn’t be necessary and may cause problems because the question being created does not have a question or any answers, it’s just a blank object. currentQuestion is set in GetRandomQuestion() from the list of questions assigned in the inspector. DisplayQuestion is only called after the random question has been chosen so, unless you have the order wrong or there are no questions, there should be a currentQuestion assigned

Thanks Bixarrio, I think I understand your point. Looking then at the order the questions are called I’ve managed to get very confused.

Here’s the first part of the QuestionSO script :slight_smile:

public class QuestionSO : ScriptableObject
{
    [TextArea(2,6)]
    [SerializeField] string question = "Enter new question text here";
    [SerializeField] string[] answers = new string[4];
    [SerializeField] int correctAnswerIndex;

    public string GetQuestion()
    {
        return question;
    }

So, ‘question’ is referring to a serializefield. Looking at Unity:

I replaced at some point the serializefield with a layout group holding the questions. So I’m not sure how those questions are being summoned.

I’m now pretty confused about how this is all hanging together. I cannot help but think some visual aids
(away from the code) would have helped to show the code flow and how components fit in and are called/ changed etc.

Any guidance would be very much appreciated.

K

When the game starts, the first Update should find that the timer has expired (it starts off expired) and then pick a new question


This goes to GetNextQuestion() where, if there are questions left, we get a new random question using GetRandomQuestion()
image
This sets the currentQuestion to a random question in the list, and removes it so that we don’t pick it again

Now currentQuestion is no longer null and we can move on to displaying it
image
DisplayQuestion() is where you had an error, but you really shouldn’t have unless there’s an issue with the logic somewhere in your code.

Thanks for being so helpful. My script mirrors yours (and indeed I checked line by line to the script in the GameDev github and could’t see a difference).

In DisplayQuestion() how does this line fit into the GetNextQuestion and GetRandomQuestion ?

questionText.text = currentQuestion.GetQuestion();

I can see that currentQuestion is defined at the start :

 QuestionSO currentQuestion;

As a QuestionSO type. Then in GetRandomQuestion() we get:

currentQuestion = questions[index];

Where questions is defined as:

 [SerializeField] List<QuestionSO> questions = new List<QuestionSO>();

What’s confusing me, I think , is that one of these ‘questionSO’ is refering to the script (hence the .GetQuestion method?) whereas there is (I think) another QuestionSO at play , but I’m unclear how that fits in - is it the component holding the questions?

I think I may have to watch this series of lectures again! There’s too many similarly named objects and I’ve lost track of what’s what.

Thanks for your help,

K

The GetNextQuestion() calls GetRandomQuestion() which puts a random question in currentQuestion. currentQuestion is one of the QuestionSO that you added to the inspector and holds each question text and its possible answers. Once that is done, DisplayQuestion displays the chosen questions question text and possible answers

There is no other QuestionSO. QuestionSO is the scriptable object that holds each question’s text and possible answers and the index to the correct answer. The Quiz.cs script gets one of these scriptable objects randomly and displays that. Then, when it’s done and you’ve answered either correctly or incorrectly (or not at all) it will move on and pick another one

Privacy & Terms