Hello,
I’m pretty new to scripting (C#) and I’m working on an App where new Prefabs are spawned.
Now I can’t find a way to make my Object, which I instantiate, as a Child.
So it’s
Instantiate(boxPrefab);
but I need it to be a child of my Panel. So my Prefab is not moving or transformed in any way.
My Prefab ist called “boxPrefab” and my Panel is called “buttonPanel” as public GameObject.
Could anyone help me out just really quickly?
Thank you!
Just set the parent transform after the prefab.
public static Object Instantiate(Object original, Transform parent);
You can also store a reference to the instantiated object and change the values after the creation of the object.
GameObject object = Instantiate(Object original, Transform parent);
Once you have a reference to your gameobject you should be able to set it’s parent with the .SetParent method.
GameObject box = Instantiate(boxPrefab);
box.Transform.SetParent(parent.transform, true);