Search Unity

Question how can i check if a variable is true or false

Discussion in 'Visual Scripting' started by xiarox123, Mar 24, 2021.

  1. xiarox123

    xiarox123

    Joined:
    Jan 4, 2020
    Posts:
    1
    how can i make that when bool variable is true or false this or that happens
    please help
     
  2. urumizawa

    urumizawa

    Joined:
    Dec 30, 2020
    Posts:
    32
    Code (CSharp):
    1. if (A && B)
    2.         {
    3.             //stuff to do if  A and B are true
    4.         }
    5.  
    6. if (!A || !B)
    7.         {
    8.             //stuff to do if either A or B are false
    9.         }
    10.  
     
    BenevolenceGames likes this.
  3. BenevolenceGames

    BenevolenceGames

    Joined:
    Feb 17, 2021
    Posts:
    128
    urumizawa has answered your question if you want multiple bools being checked for a single statement. If you only want one, we just adjust a little.

    Code (CSharp):
    1. public bool isActive = false;
    2.  
    3. void Update()
    4. {
    5.       // Here we check if isActive is true and if it is...
    6.       if (isActive)
    7.               {
    8.                      // ...then we do stuff here
    9.               }
    10.      // Since a bool can only be two values we use the else statement to do anything we want if it's false
    11.       else
    12.               {
    13.                       // Do stuff here
    14.               }
    15. }
     
  4. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,079
    The better question is why people are giving C# answers in Unity Visual Scripting subforum.
     
  5. BenevolenceGames

    BenevolenceGames

    Joined:
    Feb 17, 2021
    Posts:
    128
    Oops. I have been in a different world recently. At any rate, you'll want to right click, add variable type bool, then add a compare script and attach the bool to the bool node at the bottom of that and the true or false to their corresponding actions, and connect the input node to whatever you want to trigger that compare action.

    EDIT -- you'll want to insert a "Toggle" function to change that bool btw.