- Home /
How to check if there are 10 objects with the same tag in the scene
Is there a way to check if there are for example 10 objects in the scene with the same tag ("Answer").
I want to do something if there are 10 objects in the scene with the same tag. It should look a bit like this, I think.
if([10]ObjectsWithTag == Answer){Debug.Log("10ObjectsWithSameTag");}
So my question, how do you check if there are 10 objects with the same tag in your scene.
Answer by Mouton · Jul 06, 2015 at 09:43 AM
You can use GameObject.FindObjectsWithTag(string tag), but be carefull not to user it inside a frequently called function since it is a performance killer.
http://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html
int taggedObjectsCount = GameObject.FindObjectsWithTag("Answer").Length;
if (taggedObjectsCount >= 10)
{
Debug.Log (taggedObjectsCount + " objects with that Answer found.");
}
Answer by Daniken · Jul 06, 2015 at 12:53 PM
Check out this documentation: http://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html
In your case you want to do something like:
listWithSameTag = GameObject.FindGameObjectsWithTag("Answer");
if (listWithSameTag.size() == 10) {
// There is 10 objects with the same tag!
}
Your answer
![](https://koobas.hobune.stream/wayback/20220611230008im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Looking for a Better Way to check Multiple Tags 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Finding the position of an object 1 Answer
Finding transform.position of Object with specific Tag 0 Answers