简体   繁体   English

Unity3D:NullReferenceException即使我已经在Inspector中设置了它

[英]Unity3D: NullReferenceException even I have set it in Inspector

I am try to update GUI during Update() . 我尝试在Update()期间更新GUI。 Here is the code: 这是代码:

void Update()
    {
        Debug.Log(HPText == null);
        HPText.text = HP + " / " + MaxHP;
    }

I have already set the HPText through Inspector. 我已经通过检查器设置了HPText。
在此处输入图片说明

And I get the following in the console. 我在控制台中得到以下内容。 It seems that it keep alternating between null and not null. 似乎它在null和非null之间保持交替。 How can I get rid of the error? 如何摆脱错误?
在此处输入图片说明


Note: This is a summary answer based on my comment above. 注意:这是基于我上面的评论的简要答案。


Sounds like you have more than one object running this script. 听起来您有多个对象在运行此脚本。 Check your scene and remove the script from the second object. 检查场景,然后从第二个对象中删除脚本。 One object has the HPText property set, the other more than likely does not. 一个对象设置了HPText属性,而另一个对象则没有。

Thanks, you are right. 谢谢,你是对的。 I have 2 object using the same script. 我有2个使用相同脚本的对象。 I forget to disable the one I am not using. 我忘了禁用我不使用的那个。 – Joshua –约书亚

Alternatively, you could set the HPText on the second object. 或者,您可以在第二个对象上设置HPText You mentioned you have since disabled the second object - that works too. 您提到,此后已禁用第二个对象-也可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM