Home > Software design >  How to use multiple boolean conditions in a single if statement?
How to use multiple boolean conditions in a single if statement?

Time:01-25

I was making a puzzle where the player must push boulders to their corresponding buttons/pressure plate. I'm using booleans in a hard code for the prototype, but found myself having difficulties in what punctuation should I use between booleans? &&, and || didn't work.

Thank you for your willingness to help.

Please ignore the mess, what I need help with is the bottom public void OpenDoor in the 'if' statement, the one between trigger_1 and trigger_2 (what to replace the comma). Any advices to better the code are also appreciated.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TriggerAllButtons : UsedOnceFieldButton
{
    bool trigger_1 = false;
    bool trigger_2 = false;
    //bool trigger_3 = false;
    //bool trigger_4 = false;
    //bool trigger_5 = false;
    //bool trigger_6 = false;

    public string button_1 = "Button 1";
    public string button_2 = "Button 2";
    //public string button_3 = "Button 3";
    //public string button_4 = "Button 4";
    //public string button_5 = "Button 5";
    //public string button_6 = "Button 6";

    public string boulder_1 = "Boulder 1";
    public string boulder_2 = "Boulder 2";
    //public string boulder_3 = "Boulder 3";
    //public string boulder_4 = "Boulder 4";
    //public string boulder_5 = "Boulder 5";
    //public string boulder_6 = "Boulder 6";

    public void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag(button_1) && other.CompareTag(boulder_1))
        {
            trigger_1 = true;
        }
        if (other.CompareTag(button_2) && other.CompareTag(boulder_2))
        {
            trigger_2 = true;
        }
    //    if (other.CompareTag(button_3) && other.CompareTag(boulder_3))
    //    {
    //        trigger_3 = true;
    //    }
    //    if (other.CompareTag(button_4) && other.CompareTag(boulder_4))
    //    {
    //        trigger_4 = true;
    //    }
    //    if (other.CompareTag(button_5) && other.CompareTag(boulder_5))
    //    {
    //        trigger_5 = true;
    //    }
    //    if (other.CompareTag(button_6) && other.CompareTag(boulder_6))
    //    {
    //        trigger_6 = true;
    //    }
    }

    public void OpenDoor()
    {
        if (trigger_1 = true, trigger_2 = true) //&& (trigger_3 = true) && (trigger_4 = true) && (trigger_5 = true) && (trigger_6 = true))
        {
            UsedButton();
        }
    }
}

CodePudding user response:

You are confusing assignment = and comparison == operator. Thats why you thought the boolean operators && and || did not work.

// this is wrong
// trigger_1 and trigger_2 get true ->assigned<- before && is evaluated
// thus the "if" is always true
if (trigger_1 = true && trigger_2 = true)  

// should be like this
// trigger_1 and trigger_2 are only ->compared<- to true before && is evaluated
// this can give "if (false && true)" for example
if (trigger_1 == true && trigger_2 == true)

Same logic holds for other boolean operators (like ||). In the wrong if-statement you accidently changed the values of your bools. Side note: it is really enough to write if (trigger_1 && trigger_2), they are bools anyway.

CodePudding user response:

In my understanding you need to replace comma of open door with appropriate operator there are two common operators OR || , AND && With OR operator if only one bool is true, the condition will return true, It only return False when both bool are false With AND operator both bool need to be true in other for condition to return true.

  • Related