- Home /
How do I count or get references to all gameobjects that are tagged with a particular tag?
the Title say it all.
How do I find out how many objects have a certain tag, or get references to all gameobjects that are tagged with that particular tag?
Answer by duck · Jan 06, 2010 at 10:59 PM
Use the "FindGameObjectsWithTag" function. Here's a c# example:
// to count the number of objects: int enemyCount = GameObject.FindGameObjectsWithTag("Enemy").Length;
// to get an array of references to all those objects: GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
and in Javascript:
// to count the number of objects: var enemyCount : int = GameObject.FindGameObjectsWithTag("Enemy").Length;
// to get an array of references to all those objects: var enemies : GameObject[] = GameObject.FindGameObjectsWithTag("Enemy");
Hi
I was wondering if there was away of counting the number of objects by tag but not having an error if that tag is not found?
eg UnityException: Tag: Destroy is not defined!
This is happening becuase i haven't declared this tag YET, but i will...
thanks Sam
Answer by amin-khan · Jan 19, 2017 at 04:30 PM
in c # try this code to find out the references of all objects specific name
obsticals = GameObject.FindGameObjectsWithTag("hurdles");
if (obsticals != null)
{
foreach (GameObject roadHurdles in obsticals)
{
hurdleScripts hurdle = roadHurdles.GetComponent<hurdleScripts>();
hurdle.variable in the next scripts....= somthing;
// }
//}
Your answer

Follow this Question
Related Questions
Transform - GameObject? 1 Answer
Default GameObject Tag: Tag already registered 4 Answers
SendMessage to all *tag* within range 1 Answer