Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by moonLite · Apr 09, 2012 at 09:09 PM · if-statements

why != is different from var1 = !var2?

Hi Guys,

I'm learning the unity from the Unity 3.x game development by example. I would like to know why != is not same as var1 == !var2 & !var1 == var2

Though, I tried simple coding like if(xyz != abc), if(xyz == !abc), it looks the same to me but actual application is a different story.

I tried change the code below:

 var lastX:float; // this will stoe the last position of the character
 var isMoving:boolean = false; // flags whether or not the player is in motion
 
 function Start ()
 {
     animation.Stop (); // this will stops unity from playing character's default animation.
 }
 
 function Update () 
 {
     var halfW:float = Screen.width /2 ;
     transform.position.x = (Input.mousePosition.x)/20;
     if(lastX != transform.position.x)
     {
         //x values between this update cycle and the last one 
         //arent the same! 
         // that means the player is moving the mouse
         if(!isMoving)
         {
             // the player was standing still
             // lets flag him to "isMoving"
             isMoving = true;
             animation.Play("step");
             
             
         }
     }
     else
     {
         // the player's x position is the same as this update cycle
         // as it was the last! the player has stopped moving the mouse
         if(isMoving)
         {
             // the player has stopped moving, so lets update the flag
             isMoving = false;
             animation.Play("idle");
         
             
         }
     }    
     
     lastX = transform.position.x;
 }    

I realize that if I change the code, if(lastX != transform.position.x) to if(lastX = !transform.position.x) or if(!lastX = transform.position.x). The character won't move as expected.

So my questions are:

  1. What are the actual differences in both != vs !var?

  2. And in what kind of situations should I use != & !var?

Thanks in advance!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by save · Apr 09, 2012 at 09:42 PM

  1. The difference between them are that ! is a logical negation operator which negates its operand and != is an inequality operator which returns true when its operand aren't equal. They are both used in boolean operations but should be treated separately. ! is used for negating, != is used for comparison.

  2. ! (not) can be used in this way:

    if (!booleanValue) //If booleanValue is false
    booleanValue = !booleanValue //Set booleanValue to not be equal to booleanValue (switches between true and false)

    != (not equals to) can be used in this way:

    if (x+y != z) //If x+y isn't equal to z
    if ("apple" != "banana") //Should return true

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image moonLite · Apr 10, 2012 at 06:37 AM 0
Share

Hi save!

Thanks for the explanation, I understand the difference better. But i still have some questions about it.

1) can the !(not) use in the != (not equals to)ifelse example that you gave? what i meant it's like

   if(x+y = !z) ins$$anonymous$$d of if (x+y != z)
   if(lastX == !transform.position.x) ins$$anonymous$$d of if(lastX != transform.position.x)



From my own understanding, is that, originally, != is checking if lastX is not equal to trasn.position.X it will execute something. This part, i understand very well from your example.

2) from 1st post on top, what is the logic error behind when i change it to if(lastX == !transform.position.x) ins$$anonymous$$d of if(lastX != transform.position.x)?

3) But if i changed it to if(var 1 == !var2), unity check if lastX is equal to NOT(trasn.position.X) then it will execute something too. Isn't same as "!=" ?

4) or it depends the situation that sometimes i can use the if(x+y = !z), and sometimes i cant?

I asked because I still couldn't understand the difference in both when using if else statement with checking variables

Thanks again!

avatar image Anne_Marije · Apr 10, 2012 at 07:28 AM 1
Share

As the answer says the "!" operand negates what comes after that. so "!true" becomes "false". When you do "if(a == !b)" first the value of b is negated, and a is then compared to that.The operand(afaik) only works on a variable that can have two values, true or false. "!3" doesn't really mean anything.

avatar image moonLite · Apr 10, 2012 at 09:15 AM 0
Share

Thats the example why I couldn't understand the difference, (i came out with myself to explain to the logic why i can't understand it)

   var is$$anonymous$$oving:boolean = true;
 var hungry:boolean = false;
 
 if (is$$anonymous$$oving =!hungry)
 then John conti to move
 
 same as
 
 if (is$$anonymous$$oving != hungry)
 then John conti to move

I'm aware that coding logic is fundamentally between != and not(var). but for effects of both, i need somemore help in understanding it.

avatar image
0

Answer by by0log1c · Apr 10, 2012 at 06:58 AM

This might not be worthy of an answer, turns out it'd be easier for me to just write plenty of example and hope you deduct what does what.

 (true) == true
 (!true) == false
 (true == true) == true
 (true == false) == false
 (true != true) == false
 (true != false) == true
 (!true == false) == true
 (false == !false) == false
 (!true != !true) == false
 (!true != false) == false
 (!false == !false) == true
 (5 != 5) == false
 (5 == 5) == true
 !(5 == 5) == false
 (5 == !5) == error, the *!* operator may only be used on boolean expression.
 
 (((true == !false) != !(false != false)) == true)
 (((true) != !(false)) == true)
 ((true != true) == true)
 (false == true)
 (false)
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image moonLite · Apr 10, 2012 at 09:11 AM 0
Share

@BY0LOG1C Thanks!

Because my expression is not a boolean, it's floating values thats why it doesn't work.

but if both var is boolean, do the 2 cases make any difference?

For instance,

 var is$$anonymous$$oving:boolean = true;
 var hungry:boolean = false;
 
 if (is$$anonymous$$oving =!hungry)
 then John conti to move

same as

 if (is$$anonymous$$oving != hungry)
 then John conti to move

in this case above, it don't make a difference right?

Thanks, I do understand != is fundamentally different from var1 = !var2 but i just can't understand the different of the effects

avatar image save · Apr 10, 2012 at 10:02 AM 1
Share

$$anonymous$$ostly, for readability, you wouldn't query such a condition as once this gets nested it really is a hard trail to follow when something blows up.

It should be if (is$$anonymous$$oving == !hungry) which will return the same results as if (is$$anonymous$$oving != hungry).

You can try it out yourself with:

 private var is$$anonymous$$oving : boolean = false;
 private var hungry : boolean = false;
 
 function Start () {
     var returnDebug : String;
     for (var i : int = 0; i < 8; i++) {
         switch (i) {
             case 0:
                 is$$anonymous$$oving = true; hungry = true;
                 returnDebug = "is$$anonymous$$oving == !hungry returns "+(is$$anonymous$$oving == !hungry).ToString()+" when is$$anonymous$$oving="+is$$anonymous$$oving.ToString()+" and hungry="+hungry.ToString();
             break;
             case 1:
                 is$$anonymous$$oving = false; hungry = true;
                 returnDebug = "is$$anonymous$$oving == !hungry returns "+(is$$anonymous$$oving == !hungry).ToString()+" when is$$anonymous$$oving="+is$$anonymous$$oving.ToString()+" and hungry="+hungry.ToString();
             break;
             case 2:
                 is$$anonymous$$oving = false; hungry = false;
                 returnDebug = "is$$anonymous$$oving == !hungry returns "+(is$$anonymous$$oving == !hungry).ToString()+" when is$$anonymous$$oving="+is$$anonymous$$oving.ToString()+" and hungry="+hungry.ToString();
             break;
             case 3:
                 is$$anonymous$$oving = true; hungry = false;
                 returnDebug = "is$$anonymous$$oving == !hungry returns "+(is$$anonymous$$oving == !hungry).ToString()+" when is$$anonymous$$oving="+is$$anonymous$$oving.ToString()+" and hungry="+hungry.ToString();
             break;
             case 4:
                 is$$anonymous$$oving = true; hungry = true;
                 returnDebug = "is$$anonymous$$oving != hungry returns "+(is$$anonymous$$oving != hungry).ToString()+" when is$$anonymous$$oving="+is$$anonymous$$oving.ToString()+" and hungry="+hungry.ToString();
             break;
             case 5:
                 is$$anonymous$$oving = false; hungry = true;
                 returnDebug = "is$$anonymous$$oving != hungry returns "+(is$$anonymous$$oving != hungry).ToString()+" when is$$anonymous$$oving="+is$$anonymous$$oving.ToString()+" and hungry="+hungry.ToString();
             break;
             case 6:
                 is$$anonymous$$oving = false; hungry = false;
                 returnDebug = "is$$anonymous$$oving != hungry returns "+(is$$anonymous$$oving != hungry).ToString()+" when is$$anonymous$$oving="+is$$anonymous$$oving.ToString()+" and hungry="+hungry.ToString();
             break;
             case 7:
                 is$$anonymous$$oving = true; hungry = false;
                 returnDebug = "is$$anonymous$$oving != hungry returns "+(is$$anonymous$$oving != hungry).ToString()+" when is$$anonymous$$oving="+is$$anonymous$$oving.ToString()+" and hungry="+hungry.ToString();
             break;
         }
         Debug.Log(returnDebug);
     }
 }

Remember, ! negates, != compares.

avatar image moonLite · Apr 11, 2012 at 07:19 AM 0
Share

thanks @save !

avatar image Kleptomaniac · Apr 11, 2012 at 08:17 AM 0
Share

Haha @BY0LOG1C ... reading that was almost kind of trippy ... :D

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distance sensor 1 Answer

How to write a "or "statement & a if Else with or condition? 2 Answers

Conversion method types(void to bool)[SOLVED] 1 Answer

if statements is not working properly 1 Answer

Any way to check if an object is active? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges