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;
		}

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.

On the last line, try removing .transform