Problems/Unwanted outputs from nested if/else if/or statements.

Hey guys,
So this script below works closely with another Timer script. The goal of this game is that every 15 seconds… The goal of the level will change randomly for example:

@1M 15s:
Collect multiples of 3

@1M :
Collect multiples of 4
etc…

So the player is basically catching falling apples with numbers on them…
And to detect what multiples we are looking for I’ve used variables such as lookingformultiple 3 = true when timer <= 75s and >60s.

Now the code below that is commented out is working perfectly as desired! :smile:

So now I’m working on if the player collides with an apple with a value of 4 and it meet this condition or this condition… DO THIS.

When I uncomment all the code and let it run through it’s entirety… I don’t get the expected output…

The player collides with apple 1 when “Collect multiple of 2’s” is active, this is the output:


As you can see the 1st output is correct but I don’t see where it’s getting the 2nd output from.

C# Script Below:

    void OnTriggerEnter(Collider other)
    {
        /*if (other.gameObject.tag == "apple1")
        {
            Debug.Log("Apple 1 Wrong");
        }

        if (other.gameObject.tag == "apple2" && TimerManager2.multiple2 == true)
        {
            //ismultiple2 = true;
            //add points to slider
            //play success sound
            //ss.AddPoints();
            //SliderScript.AddPoints();
            Debug.Log("Apple 2 Right");
        }
        else if(other.gameObject.tag == "apple2" && TimerManager2.multiple2 != true)
        {
            Debug.Log("Apple 2 Wrong");
            //remove points from the slider
            //play error sound
        }

        if (other.gameObject.tag == "apple3" && TimerManager2.multiple3 == true)
        {
            Debug.Log("Apple 3 Right");
        }
        else if(other.gameObject.tag == "apple3" && TimerManager2.multiple3 != true)
        {
            Debug.Log("Apple 3 Wrong");
        }*/

        if (other.gameObject.tag == "apple4" && TimerManager2.multiple2 == true || TimerManager2.multiple4 == true)
        {
            Debug.Log("Apple 4 Right");
        }
        else if (other.gameObject.tag == "apple4" && TimerManager2.multiple2 != true || TimerManager2.multiple4 != true)
        {
            Debug.Log("Apple 4 Wrong");
        }
    }
}

I’m still getting errors with this:

        if (other.gameObject.tag == "apple4" && TimerManager2.multiple2 == true || TimerManager2.multiple4 == true)
        {
            Debug.Log("Apple 4 Right");
        }
        else if (other.gameObject.tag == "apple4" && TimerManager2.multiple2 != true || TimerManager2.multiple4 != true)
        {
            Debug.Log("Apple 4 Wrong");
        }

I don’t understand why, doesn’t “||” mean or in this if statement?

So if the object tag is apple 4 AND multiple 2 is true OR multiple 4 is true?
Then do this…?

Hotpots: hi, there

About your second message - it should work like that.

Didn’t check you code or read it but “if (a == true && b == true || c == true)” should mean that a and either/both of b and c are true.

Are you sure you don’t have multiple objects colliding in your test scene? Since one object can only have on tag, so I’d eliminate that factor first.

1 Like

Since this sounds like something that might be a typical school assignment I’m going to be a little vague in my answer and just point you in the right direction.
|| does mean OR in the statement. Your problem is that && is stronger when it comes to grouping the conditions to figure out if the entire statement is true or not. This is called operator precedence: https://msdn.microsoft.com/en-us/library/aa691323(v=vs.71).aspx

Edit: Typo, “order” should have been “operator”.

2 Likes

No, that’s not correct.

nygren:

yes you are correct. if (a == true && (b == true || c == true)) to do what I said… otherwise result is something else.

1 Like

Thanks for you help guys! :slight_smile:
I managed to find a solution by combining both of my OR statements into 1 statement as seen below:

        if (other.gameObject.tag == "apple4" && (TimerManager2.multiple2 == true || TimerManager2.multiple4 == true))
        {  
            SliderScript.AddPoints();
        }
        else if (other.gameObject.tag == "apple4" && (TimerManager2.multiple2 != true || TimerManager2.multiple4 != true))
        {
            SliderScript.RemovePoints();
        }