- Home /
How to find all transforms of cloned prefabs?
Is it even possible to uniquely identify all the transforms of the same prefab but use their copied transforms uniquely? For instance say I have 4 prefab clones in a scene that each have 4 clone child empty game objects. Is it possible to get every child's transform from each cloned prefab and use them uniquely? More specific, say prefab 1 has 4 empty game objects called spawn1, spawn2, spawn3, spawn4, but then there are 3 more prefabs exactly the same with the same names but you want to use JUST prefab 3's spawn4 and not effect the other prefabs spawn4. How would you do that.
here's a full and total explanation
http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html
please vote it up ! :)
Is there a similar C# version somewhere? Some of that I can't translate.
Answer by speedything · Oct 14, 2012 at 01:59 AM
I'm not 100% certain on what you want, but does something like this work?
void FindSpawn4()
{
GameObject cloneToUse = Instantiate(prefab, position, rotation) as GameObject;
foreach (Transform child in cloneToUse.transform)
{
if (child.name == "spawn4")
{
// do whatever here
}
}
}
Answer by aldonaletto · Oct 14, 2012 at 02:56 AM
You must have a reference to the prefab you want to use, then find the desired child with transform.Find.
The way you get the reference depends on what you're trying to do. Supposing that you've selected the prefab with a raycast, for instance, you could find a child of it this way:
if (Physics.Raycast(ray, out hit)){
Transform spawn4 = hit.transform.Find("Spawn4");
...
Another possibility is to use GetChild:
Transform spawn0 = hit.transform.GetChild(0);
...
well I've been stuck on this for about a week. I have the same prefab cloned with a Formation_AI script on it. I have my fighters go to one slot in the formation then the next and so on, but if the same prefab is cloned ($$anonymous$$ore than 1 formation) they wont go to the next formation prefab, they only go to the first 1, then when the first one is full they just stop going to any. I just need to know how to list or find all the transforms of ALL the CLONED prefabs in the scene so I can tell my fighters to go to the next ones.
Answer by kblood · Feb 06, 2013 at 12:40 PM
Not sure if it is possible for your game, but if you could give all the enemies a tag calling them enemy, you could use the tag to target them. This code is part of my Space Invaders clone, and I target the enemies, which are all clones, by their tag:
for(var theEnemy : GameObject in GameObject.FindGameObjectsWithTag("Enemy"))
{
theEnemy.transform.position.x += 2 * moveDirection;
invadersAlive = true; // This invader is still alive
if (theEnemy.transform.position.x > 50) changeDirection = true;
if (theEnemy.transform.position.x < 0) changeDirection = true;
if (theEnemy.transform.position.y < -24) gameOver = true;
if (Random.Range(0, 100) > 95)
{
// Instantiate the projectile at the position and rotation of this transform
var clone : Rigidbody;
clone = Instantiate(projectile, theEnemy.transform.position - Vector3(0, 2, 0), theEnemy.transform.rotation);
// Give the cloned object an initial velocity along the current
// object's Z axis
clone.velocity = transform.TransformDirection (Vector3.down * 10);
}
}
the Instantiate is where I make some of the enemies shoot. You could also use this to add them all to an array and control them from there, but I do not think you would even need to do so, since this already makes an array of them. Problem is if the other game objects also create the exact same prefabs. The simple but messy solution might be to just make a different prefab for each if that is possible.