- Home /
Question by
GuyFawkes · Sep 10, 2011 at 07:01 PM ·
arraysfindgameobjectswithtag
How to use FindGameObjectsWithTag
I'm trying to add 5 to the x axis of every object with a certain tag. Here's what I have so far:
for (var listofrocks : GameObject in GameObject.FindGameObjectsWithTag("Rocks"))
{
}
How would I do that? (Add 5 to the x axis of everything in the variable "listofrocks")
Comment
Best Answer
Answer by TheEmeralDreamer · Sep 10, 2011 at 09:33 PM
for (var listofrocks : GameObject in GameObject.FindGameObjectsWithTag("Rocks")) { listofrocks.transform.position.x+=5.0; }
Untested code there but I believe that's how it goes.
Answer by unluckyBastard · Sep 11, 2011 at 02:16 AM
first you have to get an array of whatever you want then you want to iterate through each variable in the array
example:
var Rocks : GameObject[]; //[] means its an array of w/e, you can use it with alot of things and it's better than Array or Arraylist...
function Update(){
Rocks = gameObject.FindGameObjectsWithTag("Rocks"); // finds all GOS with tag Rocks
for(var Rock : GameObject in Rocks){//goes through each gameobject in the array Rocks
//do stuff to each object in Rocks Here.
Rock.transform.position.x += 5;
}
}
Your answer
