How to get array of objects of type "Script" and with variable with value "X"?

I used:

foreach (EnemyHealth botCount in hazardCount) {

        if (hazardCount.myTeam == GetComponent<BaseDomain>().myTeam) {

            if (botCount.GetComponents<EnemyHealth>().Length >= hazardMax) {
                StopCoroutine(spawnWaveCoroutine);
            }

            a = botCount.GetComponents<EnemyHealth>().Length;
        }
    }

but “a” returns only one of the array, I need all of objects with script “EnemyHealth” and with team variable equals the respective base team.

Are you just trying to count something, and stop spawning once the loop has reached that number, or are you going to work with the EnemyHealth class?

“a” is only ever going to be an array of however many EnemyHealth scripts you have on the last botCount which satisfies the first condition in your foreach loop.

You could try using FindObjectsOfType. But the way you have it set up, it would probably return elements on both teams.

Why not before you run the loop, declare a List of < EnemyHealth> and when your condition is satisfied, call:

enemyHealthList.Add(botCount);

If you’re simply trying to stop spawning once you’ve reached hazardMax, a for loop is probably better suited than a foreach.