Get list of nearby targets (JS)

Hello,

I’ve been attempting to make a function that allocates all nearby gameobjects into a variable and then sorting them by range, but haven’t had much success. Would a list or array be more appropriate and how would I be able to implement it successfully?

you have 2 ways:

  1. put a trigger with a colider in the center of your GO and you can check what hit the colider
    if you are using JS

make an GO array

function OnCollisionEnter(collision :
Collision) {

array.Add(collision .gameobject);

}

if you are using c#

and for the sorting use this example

  1. get all the game objets in the scene and filter it first by the minimum distance you want
    distance and then by the distance itself.

filter any Go outside your radius

var allgameObjs : GameObject =
FindObjectsOfType(GameObject) as
GameObject;

var gameObjs : GameObject;

for(i:int=0;i radius) {

gameObjs .Add(allgameObjs*);*
>
> }
>
>
> }
then sort them like here
Sorting builtin arrays - Questions & Answers - Unity Discussions
this sould give you an idea.
i wrote this in the web so syntex might be wrong.
sfc