How to get local coordinates of gameobject?

I need to get local coordinates of gameobject like on the screen. I can see it in Editor, but I need to get it in the code. I have tried to use Transform.localPosition, but transform.position = transform.localPosition.

Hi!

I am not sure what are you trying to achieve, but localPosition and position isn’t equal, unless your GameObject don’t have any parent.

Imagine you have GameObject attached to another GameObject.

Let’s say parent GameObject has position of Vector3(0, 10, 0) and child it’s own position of Vector3(0, -10, 0).

So if you will Debug.Log(transform.localPosition) the answer from Unity will be (0, -10, 0), because it’s local position of child GameObject attached to parent GameObject. However, if you will Debug.Log(transform.position) the answer from Unity will be (0, 0, 0), because to calculate world position the engine will take into account parent position as well. So transform.localPosition is not equal transform.position, only in cases where your GameObject don’t have any parent GameObject they will be equal, but you don’t need localPosition in such cases.

If you want to find position of GameObject in 3D space projected onto screen space then you should use Camera.WorldToScreenPoint. You can find documentation about that function here: Unity - Scripting API: Camera.WorldToScreenPoint