- Home /
Count Gameobjects in Trigger with Tag x?
Hey. i wanna do something pretty simple but actually don't know how to do this. i want to count all objects with a specific tag which are inside a trigger. for example tag Player, but dont count all the other tags. and giving out the number as float how can i do this?
Answer by 3dmihai · Aug 30, 2016 at 10:47 AM
You can use something like this to store all objects in an array of gameObjects:
GameObject[] someObjects;
someObjects = GameObject.FindGameObjectsWithTag("Player");
And then you simply get the length by using someObjects.Length
Answer by davidjohn123 · Aug 30, 2016 at 02:01 PM
float count; void OnTriggerEnter(collider other) { if(other.gameobject.tag=="specific_tag"); count+=1; }
That's definitely also the solution I'd have gone for. And ofc include a block of code in OnTriggerExit that decreases the count variable. Docs: OnTriggerExit
Your answer
Follow this Question
Related Questions
Only tagged objects raise counter 1 Answer
Lap timer using colliders 1 Answer
count the number on enemyes entrering into a trigger 1 Answer
Load next level when reach score 1 Answer