简体   繁体   English

Unity行为未在检查器中设置的属性,输出null但不== null

[英]Unity behavior Property not set in inspector, prints null but doesn't == null

I have a property in a MonoBehavior, defined in the following way: 我在MonoBehavior中有一个属性,该属性按以下方式定义:

public GameObject whatever = null;

I set it to an actual GameObject reference on some of them, leave it blank in others. 我在其中一些中将其设置为实际的GameObject引用,在其他中将其留空。 Basically, its an optional property. 基本上,它是一个可选属性。 Now, the craziest thing happens when I test it in the program: 现在,最疯狂的事情发生在我在程序中对其进行测试时:

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

This prints: "null False". 打印:“ null False”。 I have no idea how to proceed. 我不知道该如何进行。 All I want is to be able to null test it in code. 我想要的是能够对代码进行空测试。 (I have tried removing the = null as well, didn't make a difference). (我也尝试过删除= null,没有任何区别)。 Unity seems to be creating some wrapper or something. Unity似乎正在创建一些包装器或其他东西。 Here is a full reduction that reproduces the problem: 这是重现此问题的完整还原:

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
    }
}

I have experienced similar behaviour when attempting to instantiate a Behaviour in Unity - there are some things ill understood going on under the hood. 尝试在Unity中实例化“行为”时,我经历了类似的行为-某些不了解的事情在后台进行。

A quick fix for this might be to refer to a specific Script instead of the entire Game Object - especially since most things you need to cross access are contained in another script (HP, for example) or are just as accessible (transform, characterController) if whatever is a MonoBehaviour 一个快速的解决方法可能是引用特定的脚本而不是整个游戏对象-尤其是因为您需要交叉访问的大多数内容都包含在另一个脚本(例如HP)中或可以访问(transform,characterController)如果有whatever是MonoBehaviour

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM