- Home /
Position variable not updating
Hey everyone! I have recently noticed a very strange issue with the transform.position setter. When setting this in a LateUpdate function I have noticed immediately after setting a value it is not the desired value. Below is a small code snippet.
LateUpdate() {
transform.localScale = new Vector3(0f, 0f, 1f);
transform.position = new Vector3(0f, 0f, 0f);
if (transform.position.x != 0) {
Debug.LogError("No change at X position: "+transform.position.x);
}
}
In this snippet, the debug statement somehow get called. Does anyone have any idea why this may be occurring?
Directly comparing floats isn't a good idea. Ins$$anonymous$$d do something like if( |a-b| < someReallySmallValue). floating point representations can be slightly off.
I've tried your code snippet on Unity 4.2.0f4, and works all right on my end; no error being logged.
I forgot to mention that it is the child of a child of an object that is moving around and being scaled. I am adjusting values based on the new scale and then setting its position back to 0.
I believe it has to do with what Jerdak says, when the object is scaled inside another object the floating point precision can be off.
Answer by IgorAherne · Aug 13, 2013 at 12:02 AM
Make sure external scripts don't change this value. Check for subtle errors. For example == and = might be playing a trick on you, and instead of comparing the variables, you change them, etc
$$anonymous$$y lookAt function is not working because transform.position doesn't update. :(
Your answer
![](https://koobas.hobune.stream/wayback/20220613112046im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why does moving a game object transform work in the WebGL build but not the Windows build? 1 Answer
transform.position = new Vector 3 NOT moving to correct position? 1 Answer
got char stuck near objects. 1 Answer
parent Transform.position unexpected behaviour when changing position 1 Answer
question about instantiating 2 Answers