- Home /
Question by
Bagul3 · Feb 14, 2014 at 12:31 AM ·
c#transform.parent
Transform.Parent not working
Hi all,
I am trying to have a shield that gets created around the player once you press "E". However only the shield gets created and does not want to follow the player. Here is my code:
public Transform shieldMesh;
if ((Input.GetKey (KeyCode.E)) &&(shieldCreated == false))
{
shieldCreated = true;
Rigidbody clone;
clone = (Rigidbody)Instantiate(shieldMesh, gameObject.transform.position,transform.rotation);
clone.transform.parent = gameObject.transform;
}
Comment
Answer by robertbu · Feb 14, 2014 at 12:59 AM
I suspect your cast of an instantiated Transform to a Rigidbody. It would work with all Transforms, or you could go with GameObjects.
public GameObject shieldMesh;
if ((Input.GetKey (KeyCode.E)) &&(shieldCreated == false))
{
shieldCreated = true;
GameObject clone;
clone = Instantiate(shieldMesh, gameObject.transform.position,transform.rotation) as GameObject;
clone.transform.parent = gameObject.transform;
}
Note since this code changes the type of 'shieldMesh', you will have to drag and drop the prefab again.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Renderer on object disabled after level reload 1 Answer
Instantantiate relative to parents position 1 Answer
Transform.parent returns only null 1 Answer