Permutation of terms

Hello!
Im seat on 2019.4.20f

I found in unity that the expression not work:
if ( (otherSlot.EquipmentSlot ==true && this.item.itemType == Item.ItemType.item) || ( this.item == null) || (otherSlot.EquipmentSlot == false))

But the expression, works:
if ((this.item == null) || (otherSlot.EquipmentSlot == true && this.item.itemType == Item.ItemType.item) || (otherSlot.EquipmentSlot == false))

Is it bug, or a feature?
According to my logic, I think this is because there is a null. (And the null operation should be important with high priority, right?)

Tell me who is understands for unity logic, because in my opinion everything looks like a permutation of the places of the terms, in which the sum does not change. In any case, it should not change… isnt it? But it turns out that unity gets stuck in the “otherSlot.EquipmentSlot == true” condition and does not check other conditions. That is, there is also the concept of “priority of operations” because of which the program produces an incorrect result. Am I missing something, or is this a mistake in unity? Found this with BAI.AI, and then work in my project.

With best regards.

It’s called short circuiting.

You can read about it in the C# docs here: Boolean logical operators - the boolean and, or, not, and xor operators - C# reference | Microsoft Learn

Basically, with the && operator, if the left hand operand returns false, it doesn’t do the right hand operand. With ||, it’s the same but opposite. If the left hand operand is true, it doesn’t bother with the right hand one.

Using a singular & or | operator will compare both sides, though it’s good practice to use the double operators as short circuiting is more performant.

Not a Unity specific thing either. It’s just a C# language thing.

3 Likes