Find all clones

Hello, I try to find all spawned enemies using

      var enemies = GameObject.FindGameObjectsWithTag("Enemy");
for( var x in enemies ) {
   x.GetComponent(EnemyAI).enemyhealth = 0;
}

But i get this error “Object reference not set to an instance of an object”.
Thanks :slight_smile:

Hello. Is GameObject set to something? Or applies the error to line 3? Do all enemy objects have an EnemyAI component?

Is that foreach loop (i dont know JS only C#)
but to prevent error you can ask if it is there
add something like this

if ( x.GetComponent(EnemyAI).enemyhealth != null);
   x.GetComponent(EnemyAI).enemyhealth=0;

i dont know if its correct but it would be of such format

Chances are you have some objects tagged enemy without an EnemyAI component.

No, all enemies are tagged with Enemy and have EnemyAI component. I solved this problem in other way.

… and the problem/solution was? :slight_smile:

Pineapples. Lots of pineapples.