- Home /
Count the number of objects with a certain tag?
Hello. How can I count the number of objects with a certain tag that are in a scene at the current time(I eventually want to set up something like "if there are > 20 objects with the tag "Enemy" do...")?Thanks
Answer by TheokieZA · Nov 29, 2010 at 12:54 PM
Hi!
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Enemy");
if(gos.length > 20)
{
// Do Something
}
Hope it helps!
Length needs to have a capital L too. I don't know if it was different back in 2010, when this response was made though.
Answer by daviddickball · Nov 23, 2017 at 09:44 AM
Or if you want to do this in just one line:
if(GameObject.FindGameObjectsWithTag("Enemy").Length > 20) {
// Do something
}
Your answer
Follow this Question
Related Questions
counting of two different objects 0 Answers
Count objects with a certain tag and display number on GUI 1 Answer
Count the contents of the list from FindGameObjectsWithTag 1 Answer
I want get gameobjects except destory obejct in loop. 1 Answer
how can i find the closest object (with the tag Point) 1 Answer