【发布时间】:2015-06-06 15:30:17
【问题描述】:
我正在检查“我的游戏对象”是否有刚体。它不是。但是,尽管刚被证明为 null,但对 Rigidbody 进行 null 检查的条件失败了。
为什么会这样?如何让我的条件块运行?
using UnityEngine;
using System.Collections;
public class NullChecker : MonoBehaviour
{
void Start()
{
GameObject go = GameObject.Find("My Game Object");
CheckIfNull<Rigidbody>(go);
}
public void CheckIfNull<ComponentType>(GameObject gameObject)
{
ComponentType component = gameObject.GetComponent<ComponentType>();
Debug.Log("Component is " + component); //"Component is null"
if (component == null)
{
Debug.Log("Inside null check"); //Never prints
}
Debug.Log("Finished null check"); //Does print
}
}
【问题讨论】: