When I log a GameObject's position to the console I get a different value in the console than in the inspector.
So, whenever I try to move the object on click, it just moves to a wild position. Am I doing anything wrong?
This is because the position you set in the inspector is the local position (that is, the position relative to the parent), while transform.position
gives you the absolute position (the position relative to the world origin).
To get the local position you can use transform.localPosition
.
ToString()
method of Vector3 objects rounds the value to the nearest 0.1
, which may also cause confusion ("Why is the debug saying it's 0? It's definitely not zero!")
Jul 25, 2017 at 16:03
In the Inspector window you see only the local transforms of gameobject.
While the Debug.Log
writes gameobject's global position in world.
Try to change the code to:
Debug.Log(transform.localPosition);