- Home /
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?
Answer by Molix · Feb 21, 2011 at 09:13 PM
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;
}
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?
@Timmyglen2: going through each one is the only possible way; that's how computers work. As $$anonymous$$olix said, just iterate through the array, it's trivial.
How would I go about this? I looked at the page, but I didn't understand any of it.
is there a quicker way $$anonymous$$e 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.
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
Your answer
Follow this Question
Related Questions
Trigger not detecting tag 1 Answer
Item pickup and activating an item afterwards help 2 Answers
Saving multiple objects with same tag 1 Answer
unity3d: making the AI jump over obstacle with a tag 1 Answer
Destroy gameobject by tag (.js) 3 Answers