How to instantiate multiple Prefabs to multiple Childs randomly ?


I’m a newbie at JavaScript and would like to know how to write a script to a GameObject which contain multiple childs, to then Instantiate to those childs from an Array of 3 Prefabs.

I successfully could instantiate randomly while applying a script to each of the childs but I want to be able to target multiple childs.

Here is the script I attached to individual childs at first:

var pArray : GameObject[];

function Start () {
	Invoke("Spawn", 0);

function Spawn(){
	var instance : GameObject = Instantiate(pArray[Random.Range(0, pArray.Length)], transform.position, transform.rotation);

Now, when I apply this script to the parent, it only works on the parent. I don’t have a clue of how to target each childs. Everything I tried lead me to errors. It’s been hours now, I’m exhausted… T_T


Use GetComponentsInChildren, then iterate through them with a for loop.

This example will find all the children + the parent himself and then child a random prefab to them. Note that GetComponentsInChildren also returns the parent, if you don’t want this you need to exclude him.

var pArray : GameObject[];
var children : Transform[] = gameObject.GetComponentsInChildren.< Transform>();
for( var child : Transform in children )
   var instance : GameObject = Instantiate(pArray[Random.Range(0, pArray.Length)], child.position, child.rotation);
   instance.transform.parent = child;


Thanks for your answer. Unfortunately, I cannot get it to work.

I get error for the second line of code:

var children : GameObject[] = gameObject.GetComponentsInChildren.< Respawns >();

Child.js(2,68): BCE0018: The name
‘Respawns’ does not denote a valid
type (‘not found’).

And then I get this error for the last line of code:

var instance.transform.parent = child.transform;

Child.js(20,21): BCE0043: Unexpected
token: …

For the second line, I entered “Respawns” which is the name of the parent than contains those Instances: “Spawner_C1”, “Spawner_C2”, and so on.

I really suck at coding so syntax is my worse enemy… sorry about that. I’m always wondering why we cannot simply write we want in plain English. Like: Move THIS object THERE WHEN this object reaches THERE. LOOP that for each of THOSE objects, TWICE.

Haha… anyway. I suck… but thanks for helping me out. :slight_smile:

**** UPDATE ****

Ok, I’ve tried to understand the GetComponentInChildren.<T> but I cannot find any information anywhere about what <T> does and how it is used. Maybe that’s one problem. No documentation about <T> in Unity, could not find anything in those forums and nothing on Google either. I don’t understand where to get info on something that looks pretty basic… or maybe those brackets are recognized as commands… I noticed those disappeared when I was about to post this update…

**** UPDATE AGAIN ****

Alright, I seem to understand that <T> means “Type” so I reverted to your original syntax but there was something wrong with it at first, which is why tried something else. Here is what it said to me at first:

var children : GameObject[] = gameObject.GetComponentsInChildren.< GameObject >();

Child.js(2,66): BCE0149: The type
‘UnityEngine.GameObject’ must derive
from ‘UnityEngine.Component’ in order
to substitute the generic parameter
‘T’ in