NullReferenceException对象不为空的技巧
根据参考资料1中的内容。
详细描述了关于 对象 不为空 的技巧。
以前这样写。
...
BoxCollider collider;
...
void start()
{
collider = this.getCompentent<BoxCollider>();
}
...
if(collider != null)
{
...
}
...
现在这样写。
...
/// <summary>
/// 当前组件的名称
/// </summary>
private string compententName = "Test.cs";
...
void start()
{
collider = this.getCompentent<BoxCollider>();
}
...
if(collider)
{
...
}
else//collider == null//collider为空的时候
{
Debug.Log("Something null." + this.transform.name + "." + this.compententName + ".var " + "text_password" + " null");
}//建议把Debug.Log改为Debug.LogWarning
...
建议把Debug.Log改为Debug.LogWarning。
有的时候 对象为空,会引起一系列的连锁错误。