- Home /
get nearest instance of game object
I need to have a function the will return a reference to the nearest instance of a prefab/game object. Nearest to my player object that is. Are there any unity functions that achieve this and if not how would i go about codeing one? Thanks in advance to any one who can help!
Answer by DaveA · Apr 03, 2011 at 08:05 PM
If they have colliders on them, you could make use of this: http://unity3d.com/support/documentation/ScriptReference/Physics.SphereCastAll.html and sort the results by distance.
$$anonymous$$aybe take a look at Physics.OverlapSphere(): http://unity3d.com/support/documentation/ScriptReference/Physics.OverlapSphere.html
Thanks $$anonymous$$an that done the trick for me,you wouldn't by any chance no how to change the variable of a script attached to the player from within another object would you.
Your answer
Follow this Question
Related Questions
gameObject.find "Player" belonging to this network player??? 1 Answer
Disable and hide object after collision between player and cube 1 Answer
NavMeshRenderer don't stop my player object when it collides with an static object 1 Answer
When Near Object, Change Scene 0 Answers
How to do something when an object is in the player sights. 2 Answers