- Home /
Question by
smirlianos · Jun 21, 2013 at 08:50 AM ·
gameobjectcomponenttagfindget
Find all gameObjects with same tag
Hello!
I have a script that is suppossed to find all objects in the scene with the tag "Bar" and change a variable of the script that they have. But it doesn't work. Any help?
var baricades = GameObject.FindGameObjectsWithTag("Bar");
var theScript = baricades.GetComponent("Js_Bars");
theScript.hitted = true;
Comment
Best Answer
Answer by moonstruck · Jun 21, 2013 at 08:59 AM
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html
This function returns the array of objects (not just one object), so change the last 2 lines to:
for( var x in barricades ) {
var theScript = x.GetComponent.< Js_Bars >();
theScript.hitted = true;
}
Like this? you are sure this is correct? it gives me errors...
var baricades = GameObject.FindGameObjectsWithTag("Bar");
foreach(var x in barricades)
{
var theScript = x.GetComponent.< Js_Bars >();
theScript.hitted = true;
}
Are you writing in JavaScript? Sorry, got used to C#, replace foreach
for for
.