- Home /
Finding another object's position?
Hello. This is a basic question, but how can I use GameObject.find in conjunction with transform.position to find the position of another object?
Answer by aldonaletto · Dec 24, 2011 at 10:26 AM
You can find any object by name using GameObject.Find("name"), then access its transform directly:
var pos = GameObject.Find("ObjectX").transform.position;
or save the GameObject reference in a variable, check if it exists, then get its position:
var gObj = GameObject.Find("ObjectX");
if (gObj){
pos = gObj.transform.position;
}
The second alternative takes a little more time, but avoids runtime NullReference errors when the object isn't found - thus should be used when the object may not exist.
Answer by skagontale · Sep 25, 2020 at 01:18 PM
public Transform object;
and then to find the coordinates put: object.position.x or object.position.y where you need to find the coordinates
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Positioning between gui and game environement 1 Answer
Return object to position 3 Answers
how to record the Vector3 position on condition? 1 Answer
Following mouse 2 Answers