- Home /
Question by
Le-Capitaine · Jan 26, 2014 at 10:06 AM ·
c#prefabfindgameobjectswithtag
How can I count the number of instances of a tagged prefab?
I cobbled this C# code together after a minute of Googling the question:
private var getCount = new Array();
void Start()
{
getCount = GameObject.FindGameObjectsWithTag ("pickup");
count = getCount.length;
}
...but neither MonoDevelop nor Unity itself seem to pick up "var" or "length" and so I'm locked out of play mode. Is there something for what I'm trying to do that, ideally, fits in one script outside the prefab?
Comment
Best Answer
Answer by robertbu · Jan 26, 2014 at 10:09 AM
Never use the Array class. FindGameObjectsWithTag() returns a build-in array, so you don't need to initialize the array. And the length of an array is 'Length' with a upper case 'L'. So you code should look like:
private GameObject[] getCount;
void Start()
{
getCount = GameObject.FindGameObjectsWithTag ("pickup");
count = getCount.Length;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613131500im_/https://answers.unity.com/themes/thub/images/avi.jpg)