Programmatically add rigidbody2d

I’m having some trouble programmatically adding a Rigidbody2D, as every time I try to I get this error: Can't add component 'Rigidbody2D' to Player because it conflicts with the existing 'BoxCollider' derived component!


Rigidbody2D rigid = player.AddComponent<Rigidbody2D> ();

I’ve even tried to destroy the BoxCollider with this piece of code (Before adding the rigidbody):

Destroy (player.GetComponent("BoxCollider"));

But nothing seems to work…

EDIT: This is my player generation code:

GameObject player = GameObject.CreatePrimitive (PrimitiveType.Cube);
player.transform.position = new Vector3 (0, sizes.height, 0);
player.renderer.material.color =; = "Player";
Destroy (player.GetComponent("BoxCollider"));
player.AddComponent<BoxCollider2D> ();
Rigidbody2D rigid = player.AddComponent<Rigidbody2D> ();
rigid.mass = 5;

You have set BoxCollider to player. Set BoxCollider2D instead first. You cant use 3d and 2d Physic component together

Had to use DestroyImmediate (player.GetComponent("BoxCollider")) instead of Destroy (player.GetComponent("BoxCollider")); as Destory only removes the BoxCollider after the frame has ended.