How can I instantiate something as a child


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 ?

Set the parent property of the transform:

InstantiatedObject.transform.parent = ParentObject.transform;

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.