Resize Array based on distance.

I've been attempting to create a targeting system that will only allow cycling through the gameObjects if they are in range. So far i can initialize the array and populate it with all gameObjects with a tag, but then i want to remove any gameObjects that are not with in a certain distance preventing the player from selecting them as a target until they are in range.

I've tried to hack it together to make it work with:

private var Master : GameObject;
private var Player : Transform;
var SelectedTarget : Transform;
var TargetList : GameObject[];
var currentTarget : int = 0;
var RadarDistance : float;

function Awake(){
Player = transform.Find("/Player");
Master = GameObject.Find("_Master");
RadarDistance = Master.GetComponent(Radar_Master).RadarRadiusRange;
SelectedTarget = null;
}

function Update(){
TargetList = GameObject.FindGameObjectsWithTag("Active");
}

function FindNextTarget(){
if(SelectedTarget == null){
    currentTarget = (currentTarget + 1) % TargetList.length;
    SelectedTarget = TargetList[currentTarget].transform;
}else{
    if((SelectedTarget.position - Player.position).magnitude > RadarDistance){
        AdvanceCurrentTarget();
    }else{
    currentTarget = (currentTarget + 1) % TargetList.length;
    SelectedTarget = TargetList[currentTarget].transform;
}}}

function AdvanceCurrentTarget(){
    currentTarget = (currentTarget + 1) % TargetList.length;
    SelectedTarget = TargetList[currentTarget].transform;
    FindNextTarget();
}

This works but it skips the first gameobject in the array that is in range. It does this because FindNExtTarget() will cycle through until it finds a target that is in range and +1. Effectively skipping the first target. I know there is a correct way of doing this, but i'm not sure how.

I was thinking of trying this but i dont know how to get it back into a new array.

TargetList = GameObject.FindGameObjectsWithTag("Active");

for (var t GameObject in TargetList){
    var dist = (Player.position - t.transform.position).magnitude;
        if(dist < RadarDistance){
            //Add gameobjects under RadarDistance to new array.
        }
    }

Any help is appreciated.

if you can spare the resources, I would probably use a separate hashtable myself for its utility, and for storing information about the target for use later.

var validTargets = new Hashtable();

For the distance check, you can use

for (var t GameObject in TargetList){
    var dist = (Player.position - t.transform.position).magnitude;
        if(dist < RadarDistance && !validTargets.ContainsKey(t)){
            validTargets.Add(t, "target info");
        }
        if(dist >= RadarDistance && validTargets.ContainsKey(t)){
            validTargets.Remove(t);
        }

    }

with a hash table you can grab the object name as a string, or some odd variables in a script of the target for use in your HUD or whatever /shrug. The issue with hashtables is that the keys aren't easily sorted, so this will require more effort in terms of your selection script grabbing a different key. It'd have to be a grab of the next from the target list and then that translated into the hash key.

Avoiding all the hash hubbub:

var validTargets = new Array();

validTargets.Clear();
for (var t GameObject in TargetList){
        var dist = (Player.position - t.transform.position).magnitude;            
        if(dist < RadarDistance){
             validTargets.Add(t);
        }                              
 }

Then you cycle through the validTargets instead of the TargetList. This is clearing and re-creating a javascript array every time, but is easier to script. If fast scripts are critical, it may be best to use built-in arrays instead, maybe make validTargets a duplicate of TargetList, where the index of invalid targets is set to null, and the index of valid targets has the object.

I started to write the full code because I feel your example is a over-complicating things a little but your second idea seems quite right. Can't you just call the below example when you need it? Then iterate through the result and pick whichever you prefer?

function FindTarget(){ TargetList = GameObject.FindGameObjectsWithTag("Active");

for(var t GameObject in TargetList){
    var sqrLen = Vector3.sqrMagnitude(Player.position - t.transform.position);
    if(sqrLen < RadarDistance*RadarDistance){
        //Debug.Log(t.name+" is nearby! To arms!
")
    }
}

}

What's the issue with "a new array"? Can we just overwrite the old one?

We could also have an array holding all enemies and a second array holding boolean for each enemies and simply turn the boolean on/off accordingly. That might ends up much cheaper cpu-speaking if you're to call FindTarget() often.