# How do I find the location of a GameObject in C#

I'm making an elevator script. Essentially I'm wanting to find the difference between two empty objects so that I can move the elevator between them.

To be able to do this, I need to be able to get the location of both GameObjects in my script. From a bit of searching, I haven't been able to figure it out.

How would I go about doing this?

• do you mean gameObject.transform.position? – Hamza Hasan Mar 17 '16 at 22:37
• Make a public game object field and use it to reference the other game object – libertylocked Mar 17 '16 at 22:38
• @LibertyLocked Yeah, I got that far. I just couldn't find the full object path for it (sorry, i think that's the wrong terminology). Probably just means that I should've spent an extra few minutes on google. – TARDIS Maker Mar 17 '16 at 22:43
• DVer, why? I can understand looking at this as low quality because of not enough research, but if you don't have this on the site already (I didn't see anything from a quick search), then it should be good to get it out there. If not, well it's a dup, and people will just have an easier time getting to an answer if they're in the same spot as me. – TARDIS Maker Mar 17 '16 at 22:48
• Seems like a valid, albeit simple question to me. @HamzaHasan You should write a complete answer. – MichaelHouse Mar 17 '16 at 23:59

Basically its all the game of terminologies, probably you couldn't find anything because of it.

Actually the basic term is Transformation which includes three basic attributes of any object, i.e. Translation( position ), Rotation and Scaling.

Have a look in detail from these answers

Right terms are so important because of there generality.

For example lets take a term Gameloop that is a general term. So you can search it for any game engine through this term, but if you know only Unity's Update method then it'd be much difficult for you find it for any other game engines. That is how the general terms work.

So your term location is not suitable though, you should use position instead when you are searching.

In Unity gameObject.transform.position will give you the global position of gameObject through its transform component.

I think that is the thing you want to know. Let me know if I did miss anything.

To get the position of a gameobject in the world, you can retrieve a Vector3 (x,y,z) of it's Position from the Transform component that's added by default to every gameobject, as seen in the inspector:

gameObject.Transform.position returns the absolute world position. Note that this is the same as GetComponent<Transform>().position on your gameobject.

gameObject.Transform.localPosition returns the position relative to that gameobject parent's position - if your gameobject has a parent whose position is not 0,0,0 this will return the Vector3 value of what you see in the inspector, rather than the absolute world position.

If you're not sure how to reference the two gameobjects in your script, the simplest and most basic way is to create two GameObject variables in your script and expose them in the editor (since they are serializable):

public GameObject OriginPosition;
[SerializeField] private GameObject _destinationPosition;


Above: Two ways of exposing a variable to the Unity Inspector

Above: The result, now you can simply drag and drop GameObjects into these slots.

UnityEngine.Debug.Log(OriginalPosition.Transform.position);
UnityEngine.Debug.Log(_destinationPosition.Transform.position);