如何修复未设置为统一对象错误实例的对象引用

时间:2019-01-30 14:03:06

标签: visual-studio unity3d

我正在Unity中创建一个问答游戏,每当在Visual Studio中调试代码时,它都不会显示任何错误,但是当我尝试统一测试代码时,会出现错误,即存在空引用代码行中的异常: questionText.text = currentQs.Question; 并且问题没有显示在面板上。

我尝试重新启动代码,但再次出现相同的错误。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;// allows me to add other methods to use the list
using UnityEngine.SceneManagement;
public class QuizManager : MonoBehaviour {

public QuestionBank[] questions;
private static List<QuestionBank> notanswered;// static to update between scenes,
private QuestionBank currentQs;

[SerializeField]//
private Text questionText;
[SerializeField]// used to add a variable for the gaps inbetween the questions
private float questionInterval = 1f;

void Start()
{
    if (notanswered == null || notanswered.Count==0)// for at the start of the this section of the game when there isn't any questions loaded into the list
    {
        notanswered = questions.ToList<QuestionBank>();
    }
    SetQuestion();
}
void SetQuestion()//function to generate a random index in the list that is linked to a question by selecting the element in Unity
{
    int RandomQsIndex = Random.Range(0, notanswered.Count);
    currentQs = notanswered[RandomQsIndex];
    questionText.text = currentQs.Question; 
}
IEnumerator NextQsTransition()
{
   notanswered.Remove(currentQs);//To remove the question the user has answered from the list
    yield return new WaitForSeconds(questionInterval);//allows us to wait for a few seconds inside the method, before something new happens.       
}

public void UserPressedTrue()
{
    if (currentQs.isTrue)
    {
        Debug.Log("Correct!");
    }else
    {
        Debug.Log("Wrong!");
    }
    StartCoroutine(NextQsTransition());
}
public void UserPressedFalse()
{
    if (currentQs.isTrue)
    {
        Debug.Log("Correct!");
    }
    else
    {
        Debug.Log("Wrong!");
    }
}

}

如果可行,应该可以将随机问题加载到我的程序中。

0 个答案:

没有答案