I whant to be able to stete several components to a group of objects with the same tag.
I also whant to be able to add a object to the transform in the script automaticly.
Can i do this? And if you can how?
1 Answer
1I didn’t understand exactly what you wanna do, but you can get all objects with a given tag using FindGameObjectsWithTag(tag), then attach whatever you want to them - like this (code attached to any object in scene):
var prefab: Transform;
function Start(){
var gOs = GameObject.FindGameObjectsWithTag("MyTag");
for (var gObj in gOs){
gObj.AddComponent(Rigidbody); // add a component like Rigidbody, for instance...
gObj.AddComponent(MouseLook); // or add a script like MouseLook.cs
// if you want to instantiate some object and child it to gObj:
var child = Instantiate(prefab, gObj.transform.position, gObj.transform.rotation);
child.parent = gObj.transform; // make gObj the child's parent
}
}
NOTE: Avoid using the GameObject.FindXxxx functions in Update - they aren’t fast, and may slow down your game.