x


How to affect multiple items with the same tag.

I am working on a script to turn on and off multiple guitexts. It is here:

var house : GameObject;
function Start () 
{
house = GameObject.FindWithTag("house");
}
function Update () 
{   
if( Input.GetKeyDown("q"))
{
house.guiText.enabled = false;
}
}

This works to turn off one of the items with the tag house, but only one. How do I change it to work for all of them?

more ▼

asked Feb 21 '11 at 09:07 PM

Timmyglen2 gravatar image

Timmyglen2
26 13 14 21

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

1 answer: sort voted first

You can use GameObject.FindGameObjectsWithTag to get a list of all of the objects with that tag; then just iterate through the list and do it to each one.

Edit: here's an example.

var houses : GameObject[];
houses = GameObject.FindGameObjectsWithTag("house"); 
// Iterate through them and turn each one off
for (var house : GameObject in houses)
{ 
  house.guiText.enabled = false;
} 
more ▼

answered Feb 21 '11 at 09:13 PM

Molix gravatar image

Molix
4.8k 16 27 66

I could do it like that, but I was trying to avoid having to go through and write a line of script that does it to each one (as I have about 20 for each different tag), is there any other way?

Feb 21 '11 at 09:46 PM Timmyglen2

@Timmyglen2: going through each one is the only possible way; that's how computers work. As Molix said, just iterate through the array, it's trivial.

Feb 21 '11 at 10:09 PM Eric5h5

How would I go about this? I looked at the page, but I didn't understand any of it.

Feb 21 '11 at 10:27 PM Timmyglen2

is there a quicker way mine seems to take a second to do it to all of them at the momment im disabling all parts of a character from kinematic to enter a rigidbody state from animation. and it just seems to jump and then do it like its taking it a second to process it all.

in an old script i had i had all the different pieces written down in a long script and then i simplified it with a loop but it seems slower, is this because its got to find all the different parts before doing it. my script is only enabled when a user presses a button. should i enable the script and make an array of the objects and then put them into a if statement.

Feb 22 '12 at 02:08 AM reptilebeats

scratch that last one, ill leave it there anyway as it may help people.. basically its late and im real slow and didnt realise that my animation was the problem not the loop, because my animation is constantly looping when my character goes into ragdoll its still trying to play the animation and takes a second to ignore it. simple destroy worked fine

Feb 22 '12 at 02:38 AM reptilebeats
(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:

x3459
x323
x133

asked: Feb 21 '11 at 09:07 PM

Seen: 1075 times

Last Updated: Feb 22 '12 at 02:38 AM