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.
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
}
}
}
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);
...
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.