Я слежу за обучающей программой Brackeys Quiz Game и столкнулся с проблемой. Я получаю сообщение об ошибке "Ссылка на объект не установлена на экземпляр объекта" для этой строки кода.
factText.text = currentQuestion.fact;
Я провожу урок с другом, и мы скопировали и вставили код, чтобы удостовериться, что наш код идентичен (ее код работает, а мой - нет, поэтому это должен быть инспектор). Проблема в том, что я не могу понять, какая ссылка отсутствует. Есть ли способ выяснить это?
Это полная ошибка.
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)
Вот мнение инспектора. Текст фактов ни к чему не привязан, но его не было в руководстве и также не было в инспекторе моего друга, насколько я могу судить, наш код и экраны идентичны. Я уверен, что что-то упустил, но не знаю, что еще попробовать.
https://imgur.com/a/3IfLEa8
Это код для Question.cs.
[System.Serializable]
public class Question {
public string fact;
public bool isTrue;
}
И это полный код 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());
}
}