- Home /
If Expression True in Immediate Window But Code In if Block Never Runs
I set a break point in my code to break whenever I click on a surface. I then enter the immediate window and test to see if the the if statement will return true in comparing two Vector3's. It does return true. However, when I step over, the code is never run as though the statement evaluated false. Does anyone know how this could be possible? I've attached a screenshot.
Here is the picture of my debug window and immediate window. You can see where the immediate window evaluates to true. The second breakpoint is not hit.
Here is the details of the two Vector3's I am comparing.
Does anyone know why I am experiencing this? It really seems like an anomaly to me :/ Does it have something to do with threading?
Answer by Huacanacha · Nov 07, 2013 at 04:46 AM
Vector3's are made up of floats which makes comparing them directly for equality a bad idea. You only see to 1 decimal place precision for the Vectors in the debug windows but one Vector could be [0.0, 0.00001, -0.99999] and the other one [0.0, 0.0, -1.0]... you may consider them close enough to be equal but mathematically they are not.
This will work in your case:
if (Vector3.Distance(hit.transform.up, upVectBefore) < 0.01)
Edit: on a closer look at the debug window, what I said is indeed correct... if you look at the individual x,y,z values they are different for each Vector. You need to decide what your tolerance level is for 'equality' and compare the vectors based on that (i.e. in the above code you need to decide to if distance should be less than 0.01 or 0.0001 etc, or alternatively look at the axis rotations and compare those within a certain tolerance).
To complement @Huacanacha's answer, have a look at $$anonymous$$athf.Approximately.
Your answer
![](https://koobas.hobune.stream/wayback/20220613121359im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do you attach the script debugger to iPhone? 3 Answers
Hitting Mono Breakpoint Crashes Unity 1 Answer
Monodevelop can't connect to iPhone for debugging 2 Answers
Monodevelop deleted my hierarchy/scene 2 Answers
What port does mono's debugger use 1 Answer