i have a couple of enemies and i want my to reference the closest one to the player position I can’t think of a simple way to do it so help please?
You can get the distance between two objects by using Vector3.Distance(), it takes two vector 3 variables, in this case the position of the player and the position of the enemy and it returns a float variable (the distance between the two objects), store the variable in a list and sort the list by the sortest distance.
for a “couple of enemies” you’ll probably find its simplest to just iterate over all the enemies and check the distance to each (Unity - Scripting API: Object.FindObjectsOfType will grab all instances of a component in the scene, i.e. the enemy script, if you haven’t stored a list of them)
with many more enemies you might consider an physics.overlapsphere check to pull off the ones nearest in the scene, before iterating over them to determine the closest.
Or, either way you get the list, you can always sort the array/list by distance using a delegate or linq