# Copy a gameObject's collider at runtime (Unity)

I'm working on a small Unity 2D project in which I need to modify the collider of the player at runtime. I'm using Physics2D.Raycast to find the collider and then what I'd like to be able to do is then copy that exact collider over to the current gameObject and remove the current collider in the process. Basically when my Ray hits an object, I want my player to turn into that object.

I'm fairly new to Unity and have been digging through the API for some hints but haven't found anything yet. Any help would be appreciated.

So far all I have is as follows:

RaycastHit2D hit = Physics2D.Raycast(transform.position, lookDirection, Mathf.Infinity, 1 << 8);
if (hit.collider != null)
{

//Change the collider somehow
Collider2D myC = GetComponent<Collider2D>();
myC = hit.collider; //Haha that doesn't work.

}

• can you provide a little bit more code? – Hamza Hasan Dec 29 '15 at 7:54
• BTW Collider2D is an abstract class so it cannot be handled like this :) – Hamza Hasan Dec 29 '15 at 8:13