- Home /
Finding position of a GameObject using its name?
I have this function:
function PrioritizeTarget() : GameObject{
//find all GameObjects with tag Ally
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Player");
var closest : GameObject;
var distance = Mathf.Infinity;
var position = transform.position;
//Iterate through to find closest
//for each Gameobject in gameobjects array
for(var go : GameObject in gos){
var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude;
if(curDistance < distance){
closest = go;
distance = curDistance;
}
}
return closest;
}
...and I am confused as to how I could find its position. It is for an AI I made and it is supposed to find the closest GameObject with the tag "Player" and track him/follow him. I can find his name (PrioritizeTarget().name)
but I dont know how to get its position as a Vector3. I am thoroughly confused
Answer by DynamicPrgm · Apr 21, 2013 at 02:49 AM
ARRRGGHHH My IQ must be 2. The solution was more than simple and I tried it even before I asked, but what i didn't notice was that when I did cmd F and replaced all of the PrioritizeTarget().name.transform.position
to PrioritizeTarget().tansform.position
I didn't notice I misspelled "Transform". I looked at the GameObject class and notice that GameObject.transform was its transform. Then I checked it and realized. FACEPALM I hope this helps future imbeciles.