Im trying to make a tower defense game and i need a tower targetting system that picks the oldest object (the enemy closest to the end) in its range and shoots at it until the object dies, then picks a new target. Each enemy object has a “age” variable that’s its age in seconds.
How do i add the enemies in range to my enemiesinrange array? I need it to return the oldest object so that the turret can target it.
The following part doesn’t compile:
if(enemiesinrange.length > 0) {
var oldestEnemy = enemiesinrange[0];
var age = Collider.age;
for (var i = 0; i < array.Count() - 1; i++) {
if (array*.age > array[oldestIndex].age) {*
oldestIndex = i;
}
}
print(oldestEnemy);
}
Here’s the code of the entire function:
function OnTriggerStay (other : Collider) {
if(other.gameObject.CompareTag(“Enemy”)){
selectedTarget = other;
isidle=0;
if(enemiesinrange.length > 0) {
var oldestEnemy = enemiesinrange[0];
var age = Collider.age;
for (var i = 0; i < array.Count() - 1; i++) {
if (array*.age > array[oldestIndex].age) {*
oldestIndex = i;
}
}
print(oldestEnemy);
}
}