HI, I have a gameobject array and i want to find the furthest gameobject in the array from my player. My player 's transform.position should be compared to each gameobject in the array and return the furthest gameobject from my player in that array . Thanks in advance!
Hi, try this script, it should work:
public GameObject[] GameObjectList;
public GameObject Player;
void Start () {
FindFurthestObject(GameObjectList);
}
GameObject FindFurthestObject(GameObject[] GameObjectList)
{
float FurthestDistance = 0;
GameObject FurthestObject = null;
foreach(GameObject Object in GameObjectList)
{
float ObjectDistance = Vector3.Distance(Player.transform.position, Object.transform.position);
if (ObjectDistance > FurthestDistance)
{
FurthestObject = Object;
FurthestDistance = ObjectDistance;
}
}
return FurthestObject;
}