【发布时间】:2015-12-01 01:11:18
【问题描述】:
所以我有两个脚本,一个叫做 EndCollider.cs,它在地图上的某个地方, 它有一个 OnTriggerEnter 函数将布尔值设置为 true。
using UnityEngine;
using System.Collections;
public class EndCollider : MonoBehaviour {
public bool isShow = false;
void OnTriggerEnter ()
{
isShow = true;
}
}
另一个脚本是SlowDownRun.cs,它在一个怪物对象上,在这个脚本中,我试图检测来自另一个脚本的布尔值是否为真,如果是则移动怪物。
using UnityEngine;
using System.Collections;
public class SlowDownRun : MonoBehaviour {
GameObject TrollScript;
EndCollider MonsterShow;
// Use this for initialization
void Start ()
{
TrollScript = GameObject.Find("Troll");
MonsterShow = TrollScript.GetComponent<EndCollider>();
}
void Update()
{
if (MonsterShow.isShow == true)
{
float movementSpeed = 10f;
transform.position += transform.forward * Time.deltaTime * movementSpeed;
}
}
}
现在代码不起作用,如果我将它们从 IF 语句中取出,移动怪物的代码就起作用了。我也一直收到这个错误 NullReferenceException: Object reference not set to an instance of an object on the line if (MonsterShow.isShow == true)
请帮助我是 Unity 的初学者,这个问题困扰了我好几个小时,我已经做了很多谷歌搜索并一次又一次地修改我的代码,但就是无法解决这个问题,我觉得很着急,只是不知道该怎么办。
【问题讨论】: