How do I remove ALL components from a gameObject?
Is it possible to remove all components in a game object, leaving behind the Transform? How can this be done?
Answer by jgodfrey · Apr 18, 2016 at 11:44 PM
I'd think this should be about right...
 foreach (var comp in gameObject.GetComponents<Component>())
 {
     if (!(comp is Transform))
     {
         Destroy(comp);
     }
 }
This doesn't work if you have components which rely on other components, Unity will fail to destroy them and log an error.
@Seanm07 Probably hacky but you could keep looping on it until they all get removed in an order Unity will accept.
What if I only want to disable the components and not destroy them? I'm attempting:
 foreach (var comp in gameObject.GetComponents())
             {
                 if (!(comp is Transform))
                 {
                     comp.GetComponent<Component>().enabled = false;
                 }
             }
but I get error:
'Component' does not contain a definition for 'enabled' and no accessible extension method 'enabled' accepting a first argument of type 'Component' could be found (are you missing a using directive or an assembly reference?)
EDIT: I finally got this to work with the following code:
    GameObject obj;
     Component[] components = obj.GetComponents(typeof(Component));
         
                     for (int i = 0; i < components.Length; i++)
                     {
                         var indexOfComponentToChange = i; // index of the component to disable
                                 
                         Behaviour castedToBehaviour = components[indexOfComponentTochange] as Behaviour;
                         if (castedToBehaviour != null)
                         {
                             castedToBehaviour.enabled = false;
                         }
                     }
A word of caution however, this disables the components on the "backend". That is; even if you manually re-enable them in the inspector, they will not re-enable. You will have to run the code again with castedToBehaviour.enabled = true; press play at least once so the code runs and it will reverse the effects.
Answer by Antan · Dec 29, 2019 at 02:23 AM
This works for me, full script below:
 using UnityEngine;
 
 [ExecuteInEditMode]
 public class RemoveAllComponentsFromGameObject : MonoBehaviour
 {
     //This is an editor script that is used to remove all components of a gameobject.
     //To use: Add this script to a gameobject
     //To clear multiple objects from components, mark multiple objects
     //and add the script to them
     
     void OnEnable()
     {
         for (int i = 0; i < 6; i++)
         { 
             foreach (var comp in gameObject.GetComponents<Component>())
             {
                 //Don't remove the Transform component
                 if (!(comp is Transform))
                 {
                     //Don't remove this script until the loop has finished
                     if (!(comp is RemoveAllComponentsFromGameObject))
                     {
                         DestroyImmediate(comp);
                     }
                 }
             }
         }
         
         if(gameObject.GetComponents<Component>().Length > 0)
             Debug.LogWarning("Tried clearing object but could not finish: " 
                 + gameObject.transform.name);
         else
             Debug.LogWarning("Cleared components from object successfully: " 
                 + gameObject.transform.name);
 
         DestroyImmediate(gameObject.GetComponent<RemoveAllComponentsFromGameObject>());
     }
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                