首页 > 解决方案 > 对象引用未设置为对象的实例,它昨天工作,今天打开并抛出错误

问题描述

基本上,我正在 Unity 中使用 Vuforia 创建一个增强现实项目。我对 C# 很陌生,所以我知道代码可能不是那么理想,但它确实有效。我昨天保存并退出了,今天当我打开它再次测试并开发更多内容时,突然抛出错误,上面写着:

NullReferenceException: Object reference not set to an instance of 
an object
marsHVB_script.Start () (at Assets/marsHVB_script.cs:11)

我有两个单独的脚本,一个是主要的:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;

public class main : MonoBehaviour
{
    public static GameObject helpObj;
    public static GameObject NVB;
    public static GameObject BVB;
    public static GameObject helpMsg;
    public static GameObject nText1;
    public static GameObject nText2;
    public static GameObject nText3;
    public static GameObject nText4;
    public static int stage = 1;
    // Start is called before the first frame update
    void Start()
    {
        helpObj = GameObject.Find("marsHVB");
        helpMsg = GameObject.Find("marsHelp");
        nText1 = GameObject.Find("nText1");
        nText2 = GameObject.Find("nText2");
        nText3 = GameObject.Find("nText3");
        nText4 = GameObject.Find("nText4");
        nText2.SetActive(false);
        nText3.SetActive(false);
        nText4.SetActive(false);
        helpMsg.SetActive(false);

    }

    public static void IncrStage()
    {
        stage++;
        Debug.Log("Stage Increased to " + stage);
    }
    public static void DecrStage()
    {
        stage--;
        Debug.Log("Stage Decreased to " + stage);
    }
    public static int GetStage()
    {
        Debug.Log("Stage Got " + stage);
        return stage;
    }
    // Update is called once per frame
    void Update()
    {

    }
}

在下面提供的代码中,我创建了应该可以从其他脚本访问的公共静态变量。我找到了它们,然后我把它们关掉了。

然后,有第二个脚本控制虚拟按钮:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;

public class marsHVB_script : MonoBehaviour, IVirtualButtonEventHandler
{

    void Start()
    {
        main.helpObj.GetComponent<VirtualButtonBehaviour>().RegisterEventHandler(this);
    }

    void IVirtualButtonEventHandler.OnButtonPressed(VirtualButtonBehaviour vb)
    {
        switch (main.GetStage())
        {
            case 1:
                main.nText1.SetActive(false);
                main.helpMsg.SetActive(true);
                break;
            case 2:
                main.nText2.SetActive(false);
                main.helpMsg.SetActive(true);
                break;
            case 3:
                main.nText3.SetActive(false);
                main.helpMsg.SetActive(true);
                break;
            case 4:
                main.nText4.SetActive(false);
                main.helpMsg.SetActive(true);
                break;
        }

        Debug.Log("Help Pressed");
    }

    void IVirtualButtonEventHandler.OnButtonReleased(VirtualButtonBehaviour vb)
    {

        switch (main.GetStage())
        {
            case 1:
                main.helpMsg.SetActive(false);
                main.nText1.SetActive(true);
                break;
            case 2:
                main.helpMsg.SetActive(false);
                main.nText2.SetActive(true);
                break;
            case 3:
                main.helpMsg.SetActive(false);
                main.nText3.SetActive(true);
                break;
            case 4:
                main.helpMsg.SetActive(false);
                main.nText4.SetActive(true);
                break;
        }
        Debug.Log("Help Removed");
    }

    // Update is called once per frame
    void Update()
    {

    }
}

第二个脚本对虚拟按钮的按下和释放做出反应。第 11 行抛出错误,这是 main.helpObj.GetComponent 函数,它昨天有效,今天无效。

请帮我解决这是什么原因,因为我现在被困在这里。

标签: c#unity3daugmented-realityvuforia

解决方案


您永远不应该使用Startvoid 来初始化代码中的变量。Awake通常用于那个,因为Awake总是在之前调用Start

如果您将班级中的 更改为Start()mainAwake()可以确保helpObj始终在marsHVB_script class尝试访问它之前分配它。


推荐阅读