I am very new to coding, I was following a tutorial and I have an error in my code. I can’t seem to find out how to solve it and as far as I can tell my code is the same as the code in the tutorial. I am getting the following error:
NullReferenceException: Object reference not set to an instance of an object
GameManager.SetCurrentQuestion () (at Assets/GameManager.cs:36)
GameManager.Start () (at Assets/GameManager.cs:28)
Here is my code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
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;
}
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());
}
}