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 '10 at 10:50 PM

Glennwiz gravatar image

Glennwiz
78 4 5 11

(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 '10 at 10:59 PM

duck gravatar image

duck ♦♦
45.8k 110 164 426

thx for help :)

Jan 07 '10 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 '12 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:

x3084
x499

asked: Jan 06 '10 at 10:50 PM

Seen: 11222 times

Last Updated: Apr 15 '12 at 12:27 PM