How to calculate the highest danger?

Hi,

I have pretty noob question here but can’t figure it out.

I’ve got a class with a float enemyThreat. My AI has a List of this class and calculates the threat of the enemy. Right now it only takes the distance into account.

My problem now is, that I can’t find a way to get the highest enemy with the threat.
My first intention was to check every object in the list and take its threat and make it the target. If the next object has a higher threat, it is the new target. This of course has the problem when the distance to the enemy increases again, the old, highest, threat value will still be set.

How can I calculate which value is the highest in a list, even when the overall threat gets smaller again?

Thanks for your help

My first intention was to check every object in the list and take its threat and make it the target. If the next object has a higher threat, it is the new target.

Close. You check against currently highest found value, and not previous value.

float currentHighestValue = 0;
GameObject targetEnemy;
for( all your objects that are in a list )
  if (enemy*.threat > currentHighestValue)*

{
targetEnemy = enemy*;*
currentHighestValue = enemy*.threat;*
}
At the end of the loop the enemy with highest threat value will be in the assigned variable (in this example, targetEnemy), regardless if enemies before or after it in a list have higher or lower values.