- Home /
parenting instantiated object
if i have a prefab such as a ball and i want to instantiate it and then parent it under a another object how do i do this to the instantiated game object and not its prefab? example:
var ball : GameObject;
Instantiate(ball,transform.position,transform.rotation) ball. transform.parent = transform
it does not work because ball is a prefab. how do i do it to the instantiated object and not the prefab it was instantiated from?
Answer by Alec-Slayden · Feb 24, 2011 at 05:54 PM
Simply assign a variable to hold the instance identity. instead of calling instantiate on its own, you can do:
var myObject : GameObject = Instantiate(ball,transform.position,transform.rotation);
myObject.transform.parent = transform;
:D Was going to write the same thing :P No issues with parenting prefabs :)
Answer by sriram90 · Feb 25, 2011 at 12:03 PM
consider you are going to make "board " as parent and "ball" as a child means....
var myObject : GameObject = Instantiate(ball,transform.position,transform.rotation);
myObject.transform.parent = GameObject.Find("board").transform;