Dynamically removing required components when destroying

Scenario:

  1. A GameObject has Component1, which requires, say, a box collider trigger.
  2. ScriptA adds Component2 – which also requires a box collider trigger – to the GameObject.
  3. ScriptB adds Component3 – which also requires a box collider trigger – to the GameObject.
  4. Later, ScriptC Destroys Component3.

In an interest in tidiness, ScriptC should destroy Component3’s required box collider. B ut the box collider shares a codependency with Component1 and Component2.

I don’t see anything in the RequiredComponent documentation that points to how I might check whether any other components also require the box collider. (i.e. programmatic access to RequiredComponent attributes of components).

Any ideas on how to best solve this problem? Would I have to eliminate the RequireComponent attribute and programmatically add required components in Start(), and track them via that method? I’m hoping I’ve failed on the research/Google front and there’s a great way to do this via baked in methods.

Your best bet is to use something else to govern the collider. I’d suggest another little script who does nothing but figure out whether his object needs a collider or not.