unity 3d student

Beginner B20 – IF Statements and Booleans

How to use IF / ELSE statements to check the status of variables and booleans to act as a switch.

Code Used (Javascript)

var myCheck : boolean = true;
  1.  
  2. function Update () {
  3.  
  4.  if(myCheck){
  5.   guiText.text = "Its on!";
  6.  }else{
  7.   guiText.text = "Its Off!";
  8.  }
  9.  
  10.  if(Input.GetButtonUp("Jump") && myCheck){
  11.   myCheck = false;
  12.  }else if(Input.GetButtonUp("Jump") && myCheck == false){
  13.   myCheck = true;
  14.  }
  15. }

Further Reading

Share via Social Media

  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Add to favorites

19 Comments

    If you’re even lazier you could also write

    if (Input.GetButtonUp (“Jump”)) {
    myCheck = !myCheck;
    }

    ..but maybe you wanted to show an example of an else-if statement ;)

    • Do you think you could explain what myCheck=!myCheck means for all of us noobs?

      • tommy – when you use != it means ‘does not equal’, so setting something to what it does not equal, in the instance of a boolean (true / false) variable, would set it to its opposite value. make sense?

        • Guess this is kinda late, but well…

          @tommy:
          myCheck = !myCheck; (regard the space between the “=” and “!”)
          That line sets the boolean myCheck to its opposite state.

          The “!” in front of a boolean is similiar to a “not” (not true ends in false, not false ends in true – guess its obvious, but just making sure).
          Thus a few lines and conditions aren’t necessary anymore.

          @wgstone
          You obviously are right about the “!=” being the opposite of “==”, but the original line of code doesn’t include either. He just skipped all spaces making everything a little messier^^

    Thanks Marcus! I’m always happy to be lazy! :)

  • Untested but it would be more simple to write the code like this.

    seriously, less lines do make a difference especially when debugging or changing code

    var myCheck : boolean = true;

    function Update () {
    guiText.text = myCheck? “Its on!”: “Its Off!”;
    myCheck = GetButtonUp(“Jump”) && myCheck ==false
    }

    • Not really, you can’t use ? in this context, but I totally agree that the simpler, and more efficient, the better of course – some of the examples on the site, especially in beginner terms are aimed at fostering understanding of the concepts rather than most efficient practice.

      • @wgstone:
        guiText.text = myCheck ? “Its on!” : “Its Off!”;
        I just quickly tested it and as i thought, it does work, even if I agree with you on it being less likely understood^^

        myCheck = GetButtonUp(“Jump”) && myCheck == false
        Whereas this DOESN’T work.

        If you want a shorter version, the code below does work:

        ———————————————————————–
        var myCheck : boolean = true;

        function Update ()
        {
        guiText.text = myCheck ? “Its On!” : “Its Off!”;
        if(Input.GetButtonUp(“Jump”)) myCheck = !myCheck;
        }
        ———————————————————————–

        The “?” checks the state of the boolean to the far left. If it’s true it sets it to the value left of the “:”, if it’s false to the one on the right (It works pretty much the same like an if-else-statement).

        • Ok but note that the one you quote that ‘DOESN’T’ work, only does not because you have a single ‘=’ in the first comparative myCheck = GetButtonUp(“Jump”) where it should be “==”.

    Sorry, me again. I’m really picky, I know.

    In you code box under the video, the four ampersands (&) are written as & . I think you’ve got a HTML error there somewhere, or it may be a browser problem (I use FF3.6.13)

  • Sorry, that should be & is written as & a m p ; without the spaces.

  • Thanks for the tutorials and quick question here. Is this statement
    really more efficient than the standard if-else or is it just more simple for some to write.

    guiText.text = myCheck? “Its on!”: “Its Off!”;

  • can someone tell my why this is only spawning an object once (on the first loop)

    var my : boolean = true;
    var baddy : GameObject;

    function Start() {
    if (my) (
    Instantiate(baddy, transform.position, transform.rotation));
    my = false;
    yield WaitForSeconds(.5);
    my = true;
    }

    • Because Start() only runs once so it’s not really a loop, make a real for loop in Update() ideally.

    Noobs, go to study JavaScript, then return here.

    • Actually this site is designed to help people learn, not exclude them.

    This tutorial should be B00, not B20 :) .

  • how do i bin other keys to script. for instance, how can i assign “f” button to switch the text? when i simply insert it instead of “Jump” it says that “f” is not setup or smth… thank you

    • either change jump to have F as its positive button in input settings – Edit > Project Settings > Input , or use GetKeyCode – see script reference.

Leave a Comment