LukaxH
LukaxH

Reputation: 71

If statement body does not execute

I have this code with this if statement. To me it looks like it's programmed right, but the if statement's code never executes. I know my variables are set to true, but it doesn't work.

Here is the Update function with the if:

void Update()
{   
    if (t0 && t2)
    {
        Debug.Log("Bingo");
        Application.LoadLevel("Bingo");
    }
}

Here's the code that sets the variables to true:

if(gameObject.name.Equals("Button 0"))
{
    t0 = true;
    Debug.Log(gameObject.name);
}

if (gameObject.name.Equals("Button 2"))
{
    t2 = true;
    Debug.Log(gameObject.name);
}

I know that these variables are set to true, though it's strange, because if I set the variabes true from somewhere else in the code, it works.

Upvotes: 1

Views: 307

Answers (2)

Avdut Kamble
Avdut Kamble

Reputation: 33

Both if statement wont be true in single execution as button name cant hold two different values.

Use || instead of &&

void Update() { if (t0 || t2) { Debug.Log("Bingo"); Application.LoadLevel("Bingo"); } }

Upvotes: 2

Ian
Ian

Reputation: 334

The two if statements will only provide one true as the 'gameObject.name' value will only ever Equal either 'Button 0' Or 'Button 2' unless they are run at least twice meeting both 'Button 0' and 'Button 2' condition you will only have one true value

Upvotes: 7

Related Questions