- Home /
How can I instantiate something as a child
Hi
I have a panel with a textfield attached to it. I also have a button that instantiates a textfield. Unfortunately if I instantiate the textfield prefab it doesn't place it as a child of the panel, but as a floating object somewhere in the world.
Is it possible to instantiate it as a child of the panel ?
Answer by tanoshimi · Jun 14, 2015 at 06:43 PM
Set the parent property of the transform:
InstantiatedObject.transform.parent = ParentObject.transform;
Hey, thank you for replying so quickly !
Although I am afraid I don't get what you mean. I've opened up a new test scene where I have 2 empty game objects. one's called parent and the other one child. I've made a prefab of the child and this is how my code looks :
if (Input.Get$$anonymous$$ouseButtonDown (0)) {
child.transform.parent = this.transform;
Instantiate(child,child.transform.position,child.transform.rotation);
}//
The instantiated object still doesn't make itself a child object of my parent.
$$anonymous$$aybe I need to put the line of code you mentioned earlier elsewhere ?
anyway thank you for your time.
you would need to do something like this: if (Input.Get$$anonymous$$ouseButtonDown (0)){ GameObject go = Instantiate(child) as GameObject; go.transform.SetParent(transform); }
Answer by sprezzatura · Jun 17, 2015 at 06:32 AM
http://docs.unity3d.com/Manual/HOWTO-UICreateFromScripting.html
Instantiating the UI element
Prefabs of UI elements are instantiated as normal using the Instantiate method. When setting the parent of the instantiated UI element, it’s recommended to do it using the Transform.SetParent method with the worldPositionStays parameter set to false.
Your answer
Follow this Question
Related Questions
Instantiating prefab at child (spawnlocations are arrays) 2 Answers
Instantiate problem with selected objects 0 Answers
Instanciate prefab responsively 1 Answer
Prefab clones behaviour 0 Answers
Picking Up Items (Inventory Implementation Problem) 0 Answers