I am working with Unity Engine where there is a base class Component
and multiple classes deriving from it, including Transform
, MeshRenderer
and many classes created by me.
I want to execute an action (in this case Destroy
all the components that aren't of types: Transform
, MeshRenderer
, MeshFilter
, Collider
and BuildMenuItem
). My basic approach looks like this:
Component[] components = obj.GetComponents<Component>();
foreach (var component in components) {
if (component is MeshFilter)
continue;
if (component is MeshRenderer)
continue;
if (component is Transform)
continue;
if (component is Collider)
continue;
if (component is BuildMenuItem)
continue;
Destroy(component);
}
In my opinion it looks ugly and the only way to shorten it I found is to use ||
operator, but it doesn't fix a lot and IMO looks even worse. Is there a cleaner way of implementing that?