- Home /
If statement is being ignored
Hi,
Somehow this part of my code is being ignorded: && Difference < 0.5f When I use == instead of > it functions properly.
void Update ()
{
float Difference = player1.transform.position.x - transform.position.x;
if (Input.GetKeyDown (KeyCode.Space) && Difference > 0.5f)
{
Debug.Log (player1.transform.position.x);
Debug.Log (transform.position.x);
Debug.Log (Difference);
}
}
The Debug.Log (Difference) shows that when Difference > 0.5f the if statement still is 'true'.
Can somebody please help me with my problem?
Thanks in advance!
You said it's still "true". How do you know that? Because of the Debug.Logs? You should use meaningful Debug.Logs
Debug.Log("$$anonymous$$eyDown: " + gameObject.name + " Diff: " + Difference + " Px: " + player1.transform.position.x + " Tx: " + transform.position.x, gameObject);
With that statement you get:
all combined in one output so it isn't mixed with other Debug logs
It has a description so it can be differentiated from other Debug.Logs
It also prints the name of the gameobject this script is attached to in case you have it on multiple gameobjects.
finally we pass "gameObject" as second "contect" parameter to Debug.Log. That should allow you by clicking (or double clicking?!?) on the Debug,Log in the console to highlight the object responsible
From your comment(s) below it seems your actual code looks different since you have admit that you have a typo in your code (which you still haven't fixed in your question). So are you sure that the code you posted really represents your actual code?
ps: There's an "edit" button in the menu on the top right of your question.
Thanks for your comment. I have not much experience with c# and what you are $$anonymous$$ching me about the functions of the Debug.Log is very useful! I'll hope I get it right now:)!
Have you checked that the script is not attached to another object in your scene? $$anonymous$$aybe the "if" is true for that object. Add a line like the next one to make sure it happens in the object you think it's happening:
Debug.Log(name);
I only have a floor, a sphere and a cylinder (the player), and the script is only attached to the sphere.. Thanks anyways, could be a very stupid mistake that could have taken me hours to solve;)
Answer by Dave-Carlile · Oct 06, 2015 at 03:53 PM
The Debug.Log (Difference) shows that when Difference > 0.5f the if statement still is 'true'
Isn't that what you have coded? "If space is pressed and Difference > 0.5f then do something". If the log is showing that Difference is > 0.5, then "Difference > 0.5)" is going to be true every time.
Sorry, was a typo in my question on this site. In my script it is: && Difference < 0.5f
Are you getting an error in the case where the difference is negative? Because -1 < 0.5 is true but may not be the logic you're looking for.
No, Difference starts at 5 as is increasing over time...
I've had Debug.Log format numbers for me (don't recall under what circumstances), but is Difference just displaying as 0.5f in the log, but it's really 0.49999f or something? I'd go with setting a break point on that line and do a watch on the value once you get there and see what the actual value is. Or try using string.Format when logging the value to make sure you're getting all of the decimal places. e.g. Debug.Log(string.Format("{0:0.00000000}", Difference))
or something like that.
$$anonymous$$y Difference value is on a certain moment about 50, and the if statement is still true (according to c#). Does it matter under those circumstances whether my float is 0.5 or 0.4999?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a bubble level (not a game but work tool) 1 Answer
C# if statement not working!! 1 Answer