Have you considered that the gameobject might have a Collider attached, and not a Collider2D? Because when there is a (3D) Collider component already, AddComponent<Rigidbody2D>() returns null. You have to call AddComponent<Rigidbody>() then!