- Home /
Create Clones as children in loops?
Hello, I have a loop and i want to make some clones as children of gameObject "spawnpoint" within the loop.
I have read alot of topics and tried lines like this:
var ino = Instantiate(blocks[Random.Range(0, blocks.Length)], Vector3 (x,y,z), rot);
//ino.transform.parent = GameObject.Find("spawnpoint").transform; ???
//ino.transform.parent = spawnpoint.transform; ???
Thanks!
Answer by SilverTabby · Aug 07, 2011 at 11:22 AM
You need to change this
//ino.transform.parent = GameObject.Find("spawnpoint").transform; ???
//ino.transform.parent = spawnpoint.transform; ???
to something more like this
//find the spawn point and store it into a var
var spawnpoint : GameObject = GameObject.Find("spawnpoint") as GameObject;
//set the parent of our instantiated object to spawn point
ino.transform.parent = spawnpoint.transform;
Also, as a side note, DO NOT use GameObject.Find() in a loop or an Update function if you can avoid it. Before the loop starts, call find ONCE and store it's result into a var, and then just use that var directly.
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
[SOLVED] How to make to clone an object and make it parent of the transform? 1 Answer
Constraining an object's movement using two spring joints on its children 1 Answer
Instantiate a Prefab as a Child? 5 Answers
How to make an object the child of another object through code? 1 Answer