x


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?

more ▼

asked Jan 06, 2010 at 10:50 PM

Glennwiz gravatar image

Glennwiz
93 12 9 17

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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");

more ▼

answered Jan 06, 2010 at 10:59 PM

duck gravatar image

duck ♦♦
48.2k 137 266 514

thx for help :)

Jan 07, 2010 at 02:23 PM Glennwiz

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

Apr 15, 2012 at 12:27 PM sam-luk
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3757
x578

asked: Jan 06, 2010 at 10:50 PM

Seen: 14052 times

Last Updated: Apr 15, 2012 at 12:27 PM