Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Difference between x =!x and x = false?

Discussion in 'Getting Started' started by Jnanda313, May 14, 2015.

  1. Jnanda313

    Jnanda313

    Joined:
    Jun 1, 2014
    Posts:
    21
    I used to think that the meant the same thing but now im not sure since i get different results. Could somebody explain the difference to me?
     
  2. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    x = false sets x to false, always. Regardless of what it was before.
    x = !x sets x to whatever it currently isn't. If it's true it goes to false, if it's false it goes to true.
     
    jhocking and Jnanda313 like this.
  3. christinanorwood

    christinanorwood

    Joined:
    Aug 9, 2013
    Posts:
    402
    You're assigning a value to x. In the first case x is assigned the opposite of what it already is. In the second case x is assigned the value false, irrespective of what it already is.
     
    Jnanda313 likes this.
  4. Jnanda313

    Jnanda313

    Joined:
    Jun 1, 2014
    Posts:
    21
    thanks! This makes it way clearer.
     
  5. Jnanda313

    Jnanda313

    Joined:
    Jun 1, 2014
    Posts:
    21
    Hey again im just a little confused about this part of a script. So i was following this tut and was wondering why
    this works:
    if(touchingWall || !touchingEdge)
    moveRight = !moveRight;


    but this does not?
    if (touchingWall)
    moveRight = true;

    if (!touchingEdge)
    moveRight = false;
     
  6. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    The first one is saying (assuming that you move left when moveRight is false, although that may not be the case):

    If you're touching a wall OR you're not touching an edge, then toggle move right. If you're currently moving right, then you will move left if either you're touching a wall, or you're not touching an edge.

    And if you were moving left, then you'll move right if you touch a wall or you're not touching an edge.

    In the second case, you're saying: if you are touching a wall, then you will move right, regardless of whether you are moving right already or not. And if you're not touching an edge, you'll move left regardless of the direction you were moving before.

    That's what the logic is saying. But I can't really speak about one working and the other not working, because I don't know exactly what you're trying to do and how your game is set up.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Assuming x is a bool, then

    Code (CSharp):
    1. x = !x;
    Is the same as

    Code (CSharp):
    1. if (x == true){
    2.     x = false;
    3. } else if (x == false) {
    4.     x = true;
    5. }
    Its just a lot less lines of code to write.
     
    krougeau and Ryiah like this.
  8. Jnanda313

    Jnanda313

    Joined:
    Jun 1, 2014
    Posts:
    21
    Thanks i kinda get it. This was the video i was following.
    Happens at 14:30
     
  9. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    Code (CSharp):
    1. if (x == true){
    2.     // ....
    Is hate too strong a word ???


    Code (CSharp):
    1. if (x ){
    2.     // .... :)
     
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    No, not at all. I was deliberately trying to be verbose to try and explain what was happening in the line x = !x.

    I would be highly unlikely to use x == true in actual code.