- Home /
 
               Question by 
               jeffaaco · Feb 25, 2013 at 06:03 PM · 
                c#if-statementsif  
              
 
              If statements not working right
I'm using a if statement to set a string, but when I hit the if statement it always evaluates to false. Unless i hard code the values in. Here's my code
 string sideString = "";
 float test = transform.position.x;
 bool testBool = side;
 Debug.Log(side); //comes back as false
 Debug.Log(testBool);//cames back as False
                 
 Debug.Log(transform.position.x); //comes Back as -1495.584
 Debug.Log(test);//comes back as -1495.584
 //string test = " ";
       
 if (!testBool && (test == (-1495.584f))) {
                     
        Debug.Log (sideString);
        sideString = "rightBottom" + roundNum.ToString ();
        Debug.Log (sideString); 
 }
then sideString will always evaluate to "".
Edit: After further testing I found its a problem with the float test, and I have also tried transform.position.x in the if statement and that didn't work.
               Comment
              
 
               
              Answer by robertbu · Feb 25, 2013 at 07:03 PM
You are trying to directly compare two floating point numbers. Given unseen digits and round-off in calculations, comparisons that seem like they should work, often fail. Use Mathf.Approximately() instead.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                