so im making chess this is part of the kings code to see if theres an enemy col on that Square. I know the fist part is true. i know how to solve it i just want to know why its not working. Thanks!
if (other.attachedRigidbody.gameObject == GameMainiger.BPieceCol[i] && other.gameObject.GetComponent<TestForCollision>().isColAvalabe
| other.attachedRigidbody.gameObject == GameMainiger.BPieceCol[i] && other.transform.parent == null && piece != null
| other.attachedRigidbody.gameObject == GameMainiger.BPieceCol[i] && piece != null && other.transform.parent.GetComponent<TestForCollision>().isColAvalabe)
{
if (!other.GetComponent<Collider2D>().attachedRigidbody.gameObject.CompareTag("WPawn"))
{
enemyCol = true;
}
}
It’s not working because there’s an error in the code. Once you fix it, it will work.
That is a horrible piece of code ^^. There are so many issues here. Not everything is necessary a logic issue but extremely hard to read or debug.
- Don’t use that many chained “dots” in a single expression. It makes the expressions quite long and hard to read. Also if there’s an issue (like a null reference) anywhere in the chain it would fail and you can’t tell where.
- You repeat a lot of conditions. If you have complex conditions, use local variables before the if statement to construct simpler conditions which you can combine.
- For some reason you used the bitwise OR operator
| instead of the conditional OR operator ||. This does change the behaviour completely since you haven’t use any parentheses for your conditions. The | operator actually has a higher precedence than the && operator. See operator precedence for reference.
The last point is most likely your issue. Though it’s kinda difficult to make any sense of the code anyways. Your condition actually reads like this:
if (other.attachedRigidbody.gameObject == GameMainiger.BPieceCol[i]
&& (other.gameObject.GetComponent<TestForCollision>().isColAvalabe | other.attachedRigidbody.gameObject == GameMainiger.BPieceCol[i])
&& other.transform.parent == null
&& (piece != null | other.attachedRigidbody.gameObject == GameMainiger.BPieceCol[i])
&& piece != null
&& other.transform.parent.GetComponent<TestForCollision>().isColAvalabe
)
This is what your condition actually looks like. So you have 5 conditions which all need to be true and two of those conditions have two options. I guess your intent was to have 3 conditions which are connected through "OR"s
I would highly recommend to break down your condition into smaller parts. A lot of your code could be simplified. I guess that “other” is a Collider / Collider2D? In that case in order to access a component on the parent object, you can simply use GetComponentInParent<>() directly on “other”.
Thanks for the feedback! I’m new to coding (as you could tell), but I’m trying to learn everything using the unity doc/ my prior knowledge of code. ngl I’ve written over a 1000 lines of code most of it kind of looks like this, but it works. ill definitely rewrite all of it using your advise!