Unity行为属性未在检查器中设置,打印为null但不是== null

时间:2014-02-06 22:51:18

标签: c# unity3d

我在MonoBehavior中有一个属性,按以下方式定义:

public GameObject whatever = null;

我将它设置为其中一些的实际GameObject引用,在其他方面留空。基本上,它是一个可选属性。现在,当我在程序中测试时,最疯狂的事情发生了:

Debug.Log(whatever + " " + (whatever == null));

打印:“null False”。我不知道该怎么办。我想要的是能够在代码中对其进行null测试。 (我也尝试删除= 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
    }
}

1 个答案:

答案 0 :(得分:0)

我尝试在Unity中实例化一个行为时遇到了类似的行为 - 有些事情被人们理解为不了解。

对此的快速修复可能是引用特定的脚本而不是整个游戏对象 - 特别是因为您需要交叉访问的大多数内容都包含在另一个脚本(例如HP)中或者只是可访问(转换) ,characterController)如果whatever是MonoBehaviour