- Home /
checking how many objects with the same tag are on screen
I have found this answer b4 but lost it again.
how do i check for how many enemies with the same tag are currentlyin the game world
what i want to do is
var enemyClones = GameObject.FindGameObjectsWithTag ("enemy");
if (enemyClones.length<1){
print ("working"); }
Answer by Proggin_Barnes · May 09, 2011 at 10:32 PM
You can put them in an array and count them with a loop.
var enemy_counter;
var enemies : GameObject[] = FindObjectsOfTag(Enemy) as GameObject[]; for (var enemy : GameObject in enemies) { enemy_counter++; } if(enemy_counter<1){ print("working"); }
GameObject.FindGameObjectsWithTag returns a list, and .length is its length. He's got the answer right in front of him. Should downvote it really.
Counting elements of a list manually is not good practice and counter productive. Use As DaveA says ( also not a clear answer DaveA, at least for non native speakers. Sarcastic answers is misleading here because obviously ( tho, it's important to point the obvious like so sometimes)
Sorry, :( I'm not an expert with unity, I was just trying to help.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133043im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Attack selected Enemy Turn Based Combat 2 Answers
Make object move in a direction depending on where it spawns? (C#) 1 Answer
Array Help 2 Answers
Way Point Assignment 4 Answers
Enemy Wave 1 Answer