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?
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.
public Transform object;
and then to find the coordinates put: object.position.x or object.position.y where you need to find the coordinates