0
\$\begingroup\$

I have other scripts in the same project using boolean if statements that work fine, but I've tried multiple methods of testing if the if statements below are working and none of them worked!

I don't really know what to ask because I don't know whats going on. I've tried placing theae statements in Update and Start, and in both instances the if statement completely ignored the check and just executed the contents of the block as though the check evaluated to true.

void OnTriggerEnter(Collider other) {
    if(other.gameObject.CompareTag("Player")); {
        working = true;
    }

    Extremity = false;
    if(Extremity); {
        Instantiate(Cannon, transform.position, transform.rotation);
        Extremity = !true;
    }
}

The false statement is only in there to show that even when the bool is set to false immediately before the check, it still runs the Instantiate method inside as though it were true.

\$\endgroup\$
1

1 Answer 1

5
\$\begingroup\$

Remove the semicolon at the end of your if statements.

That semicolon tells the compiler "I'm done with this" and whatever comes next is a new, unrelated statement

So this (note semicolon at the end of the if):

if (condition);
{
    DoStuff();
}

is the same as

if (condition)
{
    ;  // Empty statement, do nothing.
}

{ // New block, unrelated to the prior if.
    DoStuff();
}

so it says "check condition, but do nothing with the result. Then DoStuff()"

But this (note no semicolon before the opening braces):

if (condition) 
{
    DoStuff();
}

Says "check condition, and only if it's true should you execute the stuff in the braces and DoStuff()"

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .