[英]Unity 3D Find Object By Tag Issue
I have gameobject with tag "Player" assigned to it and try to find it via С# script: 我为游戏对象分配了标签“玩家”,并尝试通过С#脚本找到它:
private GameObject player1;
void Srart(){
player1 = GameObject.FindGameObjectWithTag("Player");
if (player1 == null)
Debug.LogError ("Object with player tag had not been found!");
}
void Update(){
Debug.Log (player1.name);
}
The problem is that NullReferenceException is thrown in Update() function, but LogError message is not. 问题是在Update()函数中引发了NullReferenceException,但没有LogError消息。 That means that my object was succesfully found by tag.
这意味着我的对象已成功通过标签找到。 But somehow it wasn't, because player1 in update function is equal to null.
但不知何故,因为更新功能中的player1等于null。 So why do i have this error?
那么,为什么会有这个错误?
You have a spelling error. 您有拼写错误。
Change void Srart()
to void Start()
. 将
void Srart()
更改为void Start()
。
Check out http://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html and other MonoBehaviour methods -- spelling is crucial! 查看http://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html和其他MonoBehaviour方法-拼写至关重要!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.