- Home /
subtracting numbers gives a very wrong answer
Really not sure why this is happening. I'm logging the first number, the second number, and then the same numbers subtracted, but im getting an answer that doesn't make any sense.
my code:
Debug.Log("first " + w[num].left.transform.InverseTransformPoint(array[i].transform.position).z);
Debug.Log("second " + w[num].left.transform.InverseTransformPoint(gameObject.transform.position).z);
Debug.Log("answer " + (w[num].left.transform.InverseTransformPoint(array[i].transform.position).z-w[num].left.transform.InverseTransformPoint(gameObject.transform.position).z));
An example of the output:
first 13.70678
second -6.305244
answer -2.603729
the answer should be around 20, but its -2.
Its really hard to read your code but the values could be being updated on another thread meaning that by the time you get to the third Debug.Log statement they have changed from the time you logged them out above.
Could you break your code into a more sensible/ readable structure please.
Hello. If pretend to get an asnwer, dont post this unreadable code.. modyfy it to its essentials to be easy readen by peopple. want to be sure what is happening, dont debug.code the formula, create avriables so you can DEBUGG the code:
float first = FOR$$anonymous$$ULA:
float second = FOR$$anonymous$$ULA:
float answer = first-second;
And debugg the code while running ot see the values of first and second at the answe line!
Like others have said this code is barely readable and in general bad style. Also doing gameObject.transform
is just redundant since every $$anonymous$$onoBehaviour does have a transform
property directly. The next thing is are you sure there is absolutely no code between the two lines? Threads are not an issue since accessing transform.position or InverseTransformPoint can only be used on the main thread. Any attempt do use it on a different thread would cause an artificial error by the engine.
Next are you sure the numbers you've shown are the actual numbers and not just a part of the scientific notation?
Finally once again are you sure the code you've shown is exactly the code you used? Nothing altered and nothing in between the two lines?
Just like @tormentoarmagedoom I would also highly suggest you break down your lines into clearer and smaller sub parts. In general whenever you use a value that is obtained through a more or less complex process more than once you should cache it in a variable. It doesn't even help with debugging but also improves performance.
Your answer
Follow this Question
Related Questions
Float subtracting in increments of 1 no matter what value it's subtracted by 0 Answers
Subtraction gives completely wrong answers 1 Answer
Unity Float to db 2 Answers
if(12f > 12f) = true? 2 Answers