简体   繁体   English

布尔 OR 语句返回 FALSE,即使其中一侧为 TRUE

[英]Boolean OR statement returns FALSE even though one of the sides is TRUE

This is a C# question specifically inside the Unity environment (which may or may not be relevant here).这是一个专门在 Unity 环境中的 C# 问题(在这里可能相关也可能不相关)。

I have a specific condition in a Property as follows:我在属性中有一个特定条件,如下所示:

public override bool IsFinished
{
    get
    {
        return buildable == null || (buildable != null && buildable.BuildingComplete);
    }
}

The buildable variable is an IBuildable, which in this case is a GameObject that has already been destroyed through the GameObject.Destroy method. buildable 变量是一个 IBuildable,在本例中是一个已经通过 GameObject.Destroy 方法销毁的 GameObject。

Now this condition should in this situation be true because the gameObject has been destroyed, and the buildable variable is already null.现在这个条件在这种情况下应该为真,因为游戏对象已被销毁,并且可构建变量已经为空。 However in Unity when you destroy a gameObject, you can still access that object's properties, even though the buildable == null comparison returns true.但是在 Unity 中,当您销毁游戏对象时,您仍然可以访问该对象的属性,即使buildable == null比较返回 true。

The problem is: even though buildable is actually null, the property returns false.问题是:即使 buildable 实际上为 null,该属性也会返回 false。 Proof below:证明如下:

可构建 == null

完整的条件似乎有效

该属性是错误的

Any ideas?有任何想法吗?

The default value for boolean is false.布尔值的默认值为 false。 In debugging mode, when a line is highlighted, execution is not run over it yet.在调试模式下,当一行被突出显示时,执行还没有在它上面运行。 So, that line is not executed and that expression is not computed yet.因此,该行未执行,该表达式尚未计算。 Just change that return statement to:只需将该 return 语句更改为:

var result = buildable == null || (buildable != null && buildable.BuildingComplete);
return result;

Then put a breakpoint to result line and inspect result when that breakpoint is hit.然后在结果行放置一个断点,并在命中该断点时检查结果。

Btw, as @gunr2171 pointed out in a comment, buildable != null is not needed in that expression.顺便说一句,正如@gunr2171 在评论中指出的那样,该表达式中不需要 buildable buildable != null

Thanks guys for the quick answer.谢谢你们的快速回答。 The == operator is overriden indeed in Unity for this particular one (and there's not much I can do about it because it's in the library itself). == 运算符在 Unity 中确实被这个特定的运算符覆盖(我对此无能为力,因为它在库本身中)。

在此处输入图像描述

在此处输入图像描述

I rewrote it so you can see, I understand that the default value of a boolean is false.我重写了它,所以你可以看到,我知道布尔值的默认值为 false。

在此处输入图像描述

As you can see here, even though the buildable is null, it can still be evaluated and there are actually values inside it.正如您在此处看到的,即使 buildable 为 null,它仍然可以被评估并且其中实际上有值。 The only thing I can't wrap my head around is that the left condition is STILL true (buildable == null evaluates as true) but the result is false.我唯一不能理解的是左侧条件仍然为真(可构建 == null 评估为真)但结果为假。

在此处输入图像描述

Okay I just found out some legend has already found it out.好吧,我刚刚发现一些传说已经发现了。 It's because of Unity's operator overload, so here's the solution, I just tested and it works.这是因为 Unity 的运算符重载,所以这是解决方案,我刚刚测试过,它可以工作。 Instead of using buildable == null, I'm using this method now and it works perfectly.我现在没有使用 buildable == null ,而是使用这种方法,并且效果很好。

/// <summary>
/// Returns true if the object is either a null reference or has been destroyed by unity.
/// This will respect ISPDisposable over all else.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static bool IsNullOrDestroyed(this System.Object obj)
{
    if (object.ReferenceEquals(obj, null)) return true;

    if (obj is UnityEngine.Object) return (obj as UnityEngine.Object) == null;

    return false;
}

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

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