- Home /
Instantiate with children?
Hi, I'm trying to instantiate an object (which has children objects) from prefab, but it only instantiates the parent object, and not the children.
I have two variables
public var TargetIndicator:GameObject;
public var TargetIndicatorPrefab:GameObject;
function Start () {
TargetIndicator = Instantiate(TargetIndicatorPrefab);
}
TargetIndicatorPrefab is assigned the object "TargetIndicatorGO" which has a child TargetIndicator, but when the script runs, it only instantiates "TargetIndicatorGO" with no children. What am I doing wrong? Thanks..
I am thinking you're not using the prefabs correctly.
http://docs.unity3d.com/Documentation/$$anonymous$$anual/Prefabs.html
You dragged the prefab from the project tab to the inspector correct? Be sure not to simply drag the gameobject from the scene to the inspector. I believe this will create a duplicate of the gameobject(like you are seeing) and copy only the gameobject selected.
Answer by clunk47 · Dec 26, 2012 at 09:33 PM
You don't need to use "public" for one, variables are public by default in js. Make the "clone" object (TargetIndicator) private so it doesn't show in the inspector. If you haven't done this already, you need to right click in your project pane and choose Create > Prefab. Now drag the SCENE OBJECT onto the PREFAB, and delete the original object from the scene. Now drag your PREFAB onto the slot in the inspector for Target Indicator Prefab.
private var TargetIndicator : GameObject;
var TargetIndicatorPrefab : GameObject;
function Start ()
{
TargetIndicator = Instantiate(TargetIndicatorPrefab);
}