0

I'm following Brackeys Quiz Game tutorial and have ran into an issue. I'm getting the error "Object reference not set to an instance of an object" for this line of code.

 factText.text = currentQuestion.fact;

I'm doing the tutorial with a friend and we have copied and pasted the code to make sure ours are identical (Her code works but mine doesn't so it must be the inspector). The problem is I can't figure out what reference is missing. Is there a way to figure that out?

This is the full error.

 NullReferenceException: Object reference not set to an instance of an object
 GameManager.SetCurrentQuestion () (at Assets/GameManager.cs:37)
 GameManager.Start () (at Assets/GameManager.cs:29)

Here is the view of the inspector. Fact text isn't assigned to anything, but it wasn't in the guide and also wasn't in my friend's inspector so as far as I can tell our code and screens are identical. I'm sure I'm missing something but don't know what else to try.

https://imgur.com/a/3IfLEa8

This is the code for Question.cs.

 [System.Serializable]
 public class Question {
 public string fact;
 public bool isTrue;
 }

And this is the full code of GameManager.

 using System.Collections;
 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine.SceneManagement;

 public class GameManager : MonoBehaviour
 {

public Question[] questions;
private static List<Question> unansweredQuestions;

private Question currentQuestion;

[SerializeField]
private Text factText;

[SerializeField]
private float timeBetweenQuestions = 1f;

void Start()
{
    if (unansweredQuestions == null || unansweredQuestions.Count == 0)
    {
        unansweredQuestions = questions.ToList<Question>();
    }

    SetCurrentQuestion();
}

void SetCurrentQuestion()
{
    int randomQuestionIndex = Random.Range(0, unansweredQuestions.Count);
    currentQuestion = unansweredQuestions[randomQuestionIndex];

    factText.text = currentQuestion.fact;

    unansweredQuestions.RemoveAt(randomQuestionIndex);
}
IEnumerator TransitionToNextQuestion()
{
    unansweredQuestions.Remove(currentQuestion);

    yield return new WaitForSeconds(timeBetweenQuestions);
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}

public void UserSelectTrue()
{
    if (currentQuestion.isTrue)
    {
        Debug.Log("CORRECT!");
    }
    else
    {
        Debug.Log("WRONG!");
    }
    StartCoroutine(TransitionToNextQuestion());
}

public void UserSelectFalse()
{
    if (currentQuestion.isTrue)
    {
        Debug.Log("CORRECT!");
    }
    else
    {
        Debug.Log("WRONG!");
    }
    StartCoroutine(TransitionToNextQuestion());
 }
 }
ShaneR
  • 3
  • 2
  • Assets/GameManager.cs:37 Which line is 37? – Lee Taylor May 11 '19 at 22:53
  • factText.text = currentQuestion.fact; – ShaneR May 11 '19 at 23:01
  • So, why not put a breakpoint on that line and inspect the values when it's hit? – Lee Taylor May 11 '19 at 23:02
  • `factText` must be assigned in the inspector or retrieved somehow using a `Find` + `GetComponent`. If it's not assigned in the tutorial or on your friend's inspector, they inevitably have this error too. – Hellium May 11 '19 at 23:05
  • I did and the value of factText is null just before the error. The problem is I don't fully understand what's supposed to be happening. In the tutorial the code is the same but he doesn't get the same reference error. I know the issue is with factText returning null but in the inspector there is no difference that I can see between my screen and my friend's who is sitting next to me. I would expect it to return null and can see why mine isn't working, but I can't see why it works for my friend who was following the same guide. I know I'm missing a reference somewhere but can't find it. – ShaneR May 11 '19 at 23:08
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Paul Karam May 11 '19 at 23:11

2 Answers2

0

"factText" variable is a "Text" type variable. "currentQuestion" variable is a "Question" type variable. they are different types. I think you wrote the "Question" type. Would you share the codes of the "Question" class?

uyusuk
  • 115
  • 1
  • 12
  • Editing original post with the text of Question.cs. Thank you. – ShaneR May 11 '19 at 23:00
  • fact is string but factText.text is Text type. Would you try it? factText.text.text = currentQuestion.fact; – uyusuk May 11 '19 at 23:06
  • This is the error I get when doing that. "Type `string' does not contain a definition for `text' and no extension method `text' of type `string' could be found. Are you missing an assembly reference?" – ShaneR May 11 '19 at 23:11
  • Does the currentQuestion.fact value empty when you debug? @ShaneR – uyusuk May 11 '19 at 23:15
0

The questions array is getting created in this line but doesn't get initialized anywhere in the code provided:

public Question[] questions;

So in Start() when the ToList() method runs:

unansweredQuestions = questions.ToList<Question>();

...it's going to set unansweredQuestions to null. Then when SetCurrentQuestion() tries to pull a random value from it, the NullReferenceException will occur:

currentQuestion = unansweredQuestions[randomQuestionIndex];

There seems to be some missing code that initializes the "questions" array and fills it with data.

  • That was the problem. I needed to assign Fact Text in the inspector. My confusion was that my friend DOES NOT have anything assigned in the same place in the inspector, and her code runs fine. So I confused myself by trying to copy code that somehow works when it shouldn't. I was able to get it working though, so thank you so much. – ShaneR May 11 '19 at 23:18