Enemies to target only alive players

Hello. I did almost all of Unity tutorial projects, but I’m still very new to C# and programming in general. (The easiest way to say is I’m not a programmer, but I want to learn.) Now I’m trying to create a new project by my own, but I have a few problems.

The project
-2 players in the scene
-Enemies spawned all over the place, target the closet player, and move towards it
-I got the code to get the closest player’s position from the Internet. It works fine.

The problems
-I try to make enemies only target alive player in the scene, but it doesn’t work
-The following is all the involved script

(1) I set player targets by the following function in GameManager in Awake() and call it again each time a player dies.

public void SetTargetsToEnemy () {
        playerEnemyTarget = new Transform[players.Length];
        // If a player isn't dead, set them to be enemy target
        for (int i = 0; i < playerEnemyTarget.Length; i++) {
            playerEnemyTarget [i] = players [i].instance.transform;
        }
    }

(2) In enemy script, I have GetClosestPlayer () (from the Internet), and call it in Update().
(3) But when I inserted the part that checks player current health, the console shows NullReferneceException errors.

void Update () {
        // Get the closet player position
        Transform playerTarget = GetClosestPlayer (gameManager.playerEnemyTarget);
        // Faces player
        // Moves towards player
    }
Transform GetClosestPlayer (Transform[] players) {
        PlayerStatsScript playerStats; // ***I try to check players' health here
        Transform bestTarget = null;
        float closestDistanceSqr = Mathf.Infinity;
        Vector3 currentPosition = transform.position;

        foreach (Transform potentialTarget in players) {

           // ***I try to check players' health here
            playerStats = potentialTarget.GetComponent<PlayerStatsScript> ();
            if (playerStats.currentLives <= 0)
                return null;
           // ***I try to check players' health here

            Vector3 directionToTarget = potentialTarget.position - currentPosition;
            float distanceSqrToTarget = directionToTarget.sqrMagnitude;

            if (distanceSqrToTarget < closestDistanceSqr) {
                closestDistanceSqr = distanceSqrToTarget;
                bestTarget = potentialTarget;
            }
        }
        return bestTarget;
    }

I tried to move things around but it still didn’t work. If anyone knows the reason, please tell me. Thank you very much.

potentialTarget is a Transform, try doing "potentialTarget.gameObject.GetComponent instead

Thank you very much. I change some line of the code and it works now.

Transform GetClosestPlayer (Transform[] players) {

        PlayerStatsScript playerStats;

        Transform bestTarget = null;
        float closestDistanceSqr = Mathf.Infinity;
        Vector3 currentPosition = transform.position;

        foreach (Transform potentialTarget in players) {

            playerStats = potentialTarget.gameObject.GetComponent<PlayerStatsScript> ();
            if (playerStats.currentHealth > 0) {
                Vector3 directionToTarget = potentialTarget.position - currentPosition;
                float distanceSqrToTarget = directionToTarget.sqrMagnitude;

                if (distanceSqrToTarget < closestDistanceSqr) {
                    closestDistanceSqr = distanceSqrToTarget;
                    bestTarget = potentialTarget;
                }
            }
        }
        return bestTarget;
    }

Seems you’ve already figured it out, but consider adding a function bool isAlive() to your playerStats class that returns (currentHealth > 0)

In my opinion, unless he uses it 3-4 times in his game, then it would just take up some space, and you would still need to do if (isAlive()) {}