Unity GetComponent方法中的Nullreferenceexception

时间:2015-04-01 14:03:56

标签: c# unity3d

我有一个OnTriggerEnter方法可以在玩家与其他物体碰撞时对玩家造成伤害

void OnTriggerEnter (Collider other){

    if (other.tag != gameObject.tag) {

        GetComponent<Health>().lowerHealth(other.GetComponent<Damage>().getDamage(0));

    }

}

然而,

other.GetComponent<Damage>().getDamage(0));

行给了我一个空引用异常。

这样做的正确方法是什么?

编辑:我已经创建了Damage的实例,并且还确保所有组件都在那里并且不为null。错误仍然存​​在。

0 个答案:

没有答案