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://i.stack.imgur.com/C8JIr.jpg
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());
}
}