Quantcast
Channel: Questions in topic: "parenting"
Viewing all articles
Browse latest Browse all 302

Parenting a GameObject with script not working (C#)

$
0
0
So, I'm trying to make a system where I can swap between playable characters by pressing a button, and parenting them to a GameObject with the Player Controller. However, the character object will not become a child of the player object when I create it. What's wrong with this? public GameObject hero1; public GameObject hero2; private GameObject currentHero; public Transform heroOrigin; public Transform Player; // Use this for initialization void Start () { currentHero = hero1; Instantiate(currentHero, heroOrigin.position, heroOrigin.rotation); } // Update is called once per frame void Update () { if (Input.GetAxisRaw("DPad") < -0.5 && currentHero != hero1) { currentHero = hero1; Instantiate(currentHero, heroOrigin.position, heroOrigin.rotation); currentHero.transform.SetParent(Player); } if (Input.GetAxisRaw("DPad") > 0.5 && currentHero != hero2) { currentHero = hero2; Instantiate(currentHero, heroOrigin.position, heroOrigin.rotation); currentHero.transform.SetParent(Player); } }

Viewing all articles
Browse latest Browse all 302

Trending Articles