Get ?-type Collider from child, and add duplicate component to parent

416 Views Asked by At

I'm trying to get a Collider of unknown type from a child object at run-time and add an identical Collider to the parent. How can I do that? This is the best I've got so far, but doesn't work:

Collider MColl = GetComponentInChildren<Collider>();
gameObject.AddComponent<MColl.GetType>();

"Error: 'MColl' is a variable but is used like a type"

1

There are 1 best solutions below

1
Philipp Lenssen On BEST ANSWER

You almost had it right. Use this to get it to work:

Collider collider = GetComponentInChildren<Collider>();
gameObject.AddComponent(collider.GetType()); // Assigns e.g. BoxCollider.

Good luck!