How can i use ! in this

How would i use the ! in the following statement to detect all other object names except for ENEMY, i have tried putting “!ENEMY” but it does not want to work

if (hit.transform.gameObject.name == “ENEMY”)
{
return;
}

The operator you’re looking for is the “not-equals” operator != instead of the “equals” operator ==

Alternatively any boolean value can be inverted with the “not” operator ! by placing it before the boolean expression. However the order of operations is important here. The ! operator has a higher priority than the == operator. So if you want to invert the result of the == operator, you have to use brackets

if (!(hit.transform.gameObject.name == "ENEMY"))

I would not recommend using this construct. It can be easily overlooked, is harder to understand and is just more complicated. Just use the not-equals operator

if (hit.transform.gameObject.name != "ENEMY")
1 Like

thanks for the quick reply much appreciated