I’m making a tower defense game and i need my towers to always prioritise enemies that are the closest to the end point. Each enemy has a “age” variable. I’m trying to create an array of enemies that are currently in tower’s range. How do i make the tower pick the object with the biggest “age” variable?
Something like this should work:
int oldestIndex;
for (int i = 0; i < array.Count() - 1; i++) {
if (array*.age > array[oldestIndex].age) {*
oldestIndex = i;
}
}
//Now attack the object at array[oldestIndex];
Sort the array old-to-new every few frames, then choose the object at the front of the array.