- Home /
Transform All Instances Of A Prefab
Hey guys, this problem has bugged me for hours now. You see, I have a "Loot" prefab which drops loot when an enemy dies. When I perform an action, I set the "lootMove" variable to true. Here it is basically:
if(lootMove == true)
transform.LookAt(Vector3(target.position.x, target.position.y, target.position.z));
myTransform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
Now all would be fine and dandy, but here is my problem. Let me first show you my variables:
var target : Transform;
var moveSpeed = 3;
var rotationSpeed = 3;
var myTransform : Transform;
var lootMove : boolean = false;
Now here is the killer:
function Awake()
{
myTransform = GameObject.FindWithTag("Loot").transform;
}
You see all the variables work fine with me, but the myTransform variable just doesn't seem to work. Whatever I put it as not all instances of the prefab go to the spot I want them too. It is either one of them, or none. To sum up, I want a way to set myTransform to a value that will make ALL of the instances of my "loot" prefab go to my spot. Here is what I have tried:
myTransform = GameObject.FindGameObjectWithTag("Loot")
myTransform = GameObject.FindGameObjectWithTag("Loot").transform
I also tried countless other things, but I got too lazy. Again, to make myself clear, I would like a value to set myTransform to that would make all instances of my "loot" prefab follow my transform script back at the top. Thank you :).
Answer by POLYGAMe · Feb 22, 2014 at 09:08 AM
The transform will only be set to one instance of transform with the tag loot. You can't assign multiple transforms to one transform, you're only setting it to a single one. You'll need to use arrays. Basically your transform translate code will only move whatever you set myTransform to. I'm guessing it will be the first instance of a loot tagged transform that Unity finds.
Arrays, hmmm, thank POLYGA$$anonymous$$e. I will take a look into that. :)