Swapping collider types programmatically

I’m trying to create an editor script that swaps one collider type with another, but the script is getting hung up on game objects that include a collider-requiring component.

For example, lets say I have a “Chair” object with two components: a SphereCollider, and a CanNotRemoveColliders.cs script which includes the attribute [RequireComponent(typeof(Collider))]. When my script attempts to remove the chair’s SphereCollider and add a BoxCollider in its place, it throws Unity’s “Can’t remove component” error and can’t finish the job.

I’ve tried programmatically adding a second collider to the object (which is allowed, oddly) and then destroying the first collider, however Unity still gets hung up on the original collider - the error is still thrown.

Is there any way to swap collider components directly, or to suppress the “Can’t remove component” error?

I would suggest getting rid of the RequiresComponent attribute and instead perform a check at Start() to create a collider if none exists and toss out a Debug Error and break if said component is removed.

Something like

public string colliderType; //a string with BoxCollider or SphereCollider or whatnot
Collider collide;

void Start()
{
 collide = collider;
 if(collide == null)
 {
  Debug.LogError("No Collider")
  Debug.Break();
  //OR
  gameObject.AddComponent(colliderType);
 }
}

Then check collide for null before you use it, and place an else statement to create said component if it’s removed.

in short, remove the RequireComponent functionality and instead use a similar but custom set of code to replicate it, while allowing you to swap said component.

Thanks, but I’m afraid that my collider editor script will need to account for the RequireComponent attribute since the editor will be used in projects that I’m unlikely to have direct involvement in. I’m creating a generalized set of tools that will hopefully function consistently regardless of how the current project is set up.

This is how I did it:

@script RequireComponent(BoxCollider)
function Awake(){
	if (GetComponent(SphereCollider)) Destroy(GetComponent(SphereCollider));
}

if one of the coliders is big enough to completely cover the other colider then set the smaller colider to the default and spawn the bigger colider over the first through the script.

Just to clarify, I’m creating an editor script, and I can make no assumptions about how the scene has been set up or how the in-game scripts have been written. These may be impossibly strict requirements, but I’m hoping that there’s a solution that falls within Unity’s public editor API.

The only option I see is to create a new GameObject, add your collider and then reconstruct the entire tree of the source object (by using reflection to copy all public values of the attached scripts and components) and then replace the source game object by the new one.

Quite some work.

On a side note: this kind of thing is exactly why I avoid RequireComponent like the plague.

Using reflection to rebuild the object is a really intriguing idea, but probably more work than it’s worth. Thanks for the suggestions. It looks like I’ll have to go without full-featured collider replacement.