- Home /
How to instantiate multiple Prefabs to multiple Childs randomly ?
Hello!
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
Thanks.
Answer by Joshua · Jul 11, 2011 at 03:50 AM
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;
}
Well, thanks for helping but that still doesn't work. There seems to be a problem with a portion of the code you provided. Something seems to be missing. I tried to fix it myself but got fed up over 10 $$anonymous$$utes. I tried to remove the parenthesis after "child.rotation" or add one before it. No results. Tried to put as comment to try to isolate the problem. No results.
It always gets me this kind of error:
Test.js(7,1): BCE0043: Unexpected token: .
Sorry about that. (-_-)
Well it seems that this buggy site was not displaying my code properly :s half of it was simply not displayed.. so it's not your nor my fault. Should work now.. =\
Hmm... I don't know what's happening but a clean copy / paste gives me this error when I save:
Test.js(6,16): BCE0043: Unexpected token: ..
Phew... (>_<)
Update **
I even retyped the whole thing from scratch and it gave me a similar error:
Test.js(6,21): BCE0043: Unexpected token: ..
It doesn't like something on the last line I think...
Ok, I finally managed to make it work but there is an error on this line:
var instance.transform.parent = child;
I removed "var" and now it works. It's not exactly what I had in $$anonymous$$d though, as I have to manually define the children. But I'll work with it until I discover how to do it. Thanks Joshua!
Answer by pcote · Jul 11, 2011 at 06:49 PM
Hello!
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. ^_^
UPDATE
Ok, I've tried to understand the GetComponentInChildren.`` 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 'UnityEngine.GameObject.GetComponentsInChildren(boolean)'.
Hey there, in the future, please do not post comments as answers.
It could be that I was wrong about using GameObject as type, try transform. Other then that it all looks fine. I'll edit my original answer to show you how it should look. You need not change anything, except fill in the first array.
Oops, I didn't know how the system worked. Thanks for telling me about it!
Ok, I'll try with your update to see is the result. Thanks!
I got the code to work, but wanted to know how to make sure no duplicates are instantiated. Please help thanx.