- Home /
Instantiate a Prefab as a Child?
Hi! So I'm making this game and I need to Instantiate an arm next to my Player and I want it to follow him as if it belongs to him, in other words : make it a child of my player.
I already got the Instantiate part covered, the thing is I can't seem to make the arm follow my player around and it's been quite frustrating ;-;
I'm a beginner to Unity and Programming, so if you guys have an answer I would appreciate if you printed the code here, so I could use it in my scripts :)
Answer by LCStark · Dec 21, 2018 at 06:30 AM
Check the Object.Instantiate documentation. Instantiate
has one variant which will be of use in your situation:
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);
Just pass your
parent
transform as the fourth parameter.
Answer by thapliyalankit · Dec 21, 2018 at 05:39 AM
Refer the following code-
public Canvas Parent;
public Button Child;
void Start(){
Button btn=Instantiate(Child) as Button;
btn.transform.SetParent(Parent.transform, false);
}
Answer by Cornelis-de-Jager · Dec 20, 2018 at 10:59 PM
public Transform body;
public GameObject armPrefab;
void SpawnArm () {
GameObject arm = Instantiate(armPrefab, SpawnLoc, SpawnRot) as GameObject;
arm.transform.parent = body;
}
Answer by ChristopherGoe · Dec 20, 2018 at 10:41 PM
So you said you already got the Instantiate part covered, i just call the object arm for now one.
arm.transform.SetParent(Player);
Your answer
![](https://koobas.hobune.stream/wayback/20220612184331im_/https://answers.unity.com/themes/thub/images/avi.jpg)