Delete all component but not Renderer/MeshFilter (at runtime)

I would destroy all the components of a gameobject except Transform (of course), the Renderer and MeshFilter …

In practice I instantiate my object, but then I would take away all the scripts that are not those three.

How can I do?

My try:

 void CreateMiniMesh () {
     ObjectMesh= GameObject.Instantiate(MyOriginalObject);
     Component[] AllChidren= ObjectMesh.transform.GetComponentsInChildren<Component>();
     foreach (Component childrens in AllChidren) {
         if(!childrens.GetComponent<Renderer>()&&
            !childrens.GetComponent<Transform>()&&
            !childrens.GetComponent<MeshFilter>()){
             Destroy(childrens.GetComponent<Behaviour>());
         }
     }
}

You are close. Some pseudo code for runtime type checking

foreach (Component child in AllChidren) {
     if(child.GetType() != typeof(Transform)){
         Destroy(child);
     }
}

Thanks, now I try :slight_smile:

EDIT:

Many thanks, it works perfectly :slight_smile:
Now I can replicate a spaceships for the GUI render without all superfluous script, as rigidbody, AI scripts etc …

Thanks again.