c# - 对象引用未设置为对象的实例,它昨天工作,今天打开并抛出错误
问题描述
基本上,我正在 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 函数,它昨天有效,今天无效。
请帮我解决这是什么原因,因为我现在被困在这里。
解决方案
您永远不应该使用Start
void 来初始化代码中的变量。Awake
通常用于那个,因为Awake
总是在之前调用Start
。
如果您将班级中的 更改为Start()
,main
则Awake()
可以确保helpObj
始终在marsHVB_script class
尝试访问它之前分配它。
推荐阅读
- ios - 使用 PromiseKit 时更新领域对象的问题
- html - 如何让输入字段出现在 html 中的画布顶部
- animation - gnuplot:交互式 3D 动画?
- java - 在 Gradle 项目中使用 Kotlin 进行条带集成
- javascript - 如何避免在下拉菜单上刷新
- python - 该网站是否受到保护以防刮擦?
- amazon-web-services - 我们可以为 AWS DAX 项目缓存设置无限 TTL 吗?
- c# - 带有 .net core 2.2 的 Docker:来自守护进程的错误响应:OCI 运行时创建失败
- dart - 从 Flutter TextField 中读取值
- c# - 如何解决 IBM DB2 数据库的连接问题