【发布时间】:2014-02-06 22:51:17
【问题描述】:
我在 MonoBehavior 中有一个属性,定义如下:
public GameObject whatever = null;
我在其中一些上将其设置为实际的 GameObject 引用,而在另一些上将其留空。基本上,它是一个可选属性。现在,当我在程序中测试它时,最疯狂的事情发生了:
Debug.Log(whatever + " " + (whatever == null));
这打印:“null False”。我不知道如何进行。我想要的只是能够在代码中对其进行空测试。 (我也尝试过删除 = null ,但没有任何区别)。 Unity 似乎正在创建一些包装器或其他东西。这是重现问题的完整缩减:
using UnityEngine;
using System.Collections;
public class TestBehavior : MonoBehaviour
{
public GameObject whatever;
// Use this for initialization
void Start ()
{
Debug.Log("logical: " + whatever + " " + (whatever == null)); // prints null True
Print(whatever);
}
public void Print<T>(T anObject)
{
Debug.Log("Now it gets nuts: " + anObject + " " + (anObject == null)); // prints null False
}
}
【问题讨论】:
-
null在连接期间被视为空字符串,因此它应该输出“false”.. -
另外,您不应该将泛型参数与
null进行比较,请参阅stackoverflow.com/a/864860/238419。不过,这仍然不会导致您看到的问题..