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;

1 Answer

1

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.

Thanks, now it works!