Suggestions for AI Selecting From Multiple Targets?

I am setting up AI state system for my game and I have all the normal functions that an action battle system would have: Pathfinding, Movement, and Attacking . This system of mind is working well (For a one player game).

However, I will be having the player be assisted by 3 other partner AI (using the same scripts). Now, the problem is, the enemy's are locked on to the player object only, and the partner AI are targeting only the one enemy entity selected in the inspector.

I was wondering on what approach I should go towards getting the enemy to select a target between the 3-4 players and vise-versa for the partner AI.

So far, I have code from my targeting system that allows the player to find all of the closest enemies and selects it as a target. I am not sure how to get the enemy to use this same function.

var closest : GameObject;

function FindClosestEnemy () : GameObject 
    {
        // Find all game objects with tag Enemy
        enemyLocations = GameObject.FindGameObjectsWithTag("Enemy"); 
        //var closest : GameObject; 
        var distance = Mathf.Infinity; 
        var position = transform.position; 
        // Iterate through them and find the closest one
        for (var go : GameObject in enemyLocations) 
            { 
                var diff = (go.transform.position - position);
                var curDistance = diff.sqrMagnitude; 

                if (curDistance < distance) 
                { 
                    closest = go; 
                    distance = curDistance; 
                } 
            } 
        return closest; 
    }

I would conclude that maybe the following would be appropriate to make an enemy/partner change there target?

-Closest Enemy -Health Checking -Stat Checking (IDK) -Death?

Would a timer suffice for changing targets?

I would also need the Partners to set the player as there target when I'm fleeing from enemies or when the enemies are all dead.

Basically you can use the same function for enemy AI. You will just have to modify the tag it's searching for. I would just change the signature of the function to FindClosestEnemy(string tag) and use the same function for players and enemies.

Hello there, what about the attack script to be include to that script so that when it finds multiple targets it would immediately attack them by projectiles. Please help me with this. because i’m having trouble in using this script.

void DistanceToTarget()
{
//Sort so nearest enemy is always Enemies[0]
PlayerUnits.Sort(delegate( Transform t1, Transform t2){
return Vector3.Distance(t1.position,transform.position).CompareTo(Vector3.Distance(t2.position,transform.position));
});
}

void TargetedEnemy()
	{
		DistanceToTarget();
		//after sorted nearest enemy will be 0
		SelectedTarget = PlayerUnits[0];
	}

This can handle around 80 units for both players/enemies if done right with collisions. If someone knows of a better optimized way please let me know!