Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by brain56 · Apr 18, 2016 at 11:12 PM · gameobjecttransformcomponent

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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);
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Seanm07 · Oct 12, 2017 at 10:28 AM 0
Share

This doesn't work if you have components which rely on other components, Unity will fail to destroy them and log an error.

avatar image JeromeJ · Mar 12, 2018 at 10:13 PM 0
Share

@Seanm07 Probably hacky but you could keep looping on it until they all get removed in an order Unity will accept.

avatar image GoblinGameWorks · Mar 13 at 04:08 AM 0
Share

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.

avatar image
0

Answer by CapricornGaikov · Feb 28, 2019 at 10:36 AM

How this can be done via editor?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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>());
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

69 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Assets/CArSmoothFollow.cs(40,68): error CS0246: The type or namespace name `ExampleGame_Game' could not be found. 1 Answer

Auto level an object 0 Answers

Instantiated objects can't convert to GameObjects 0 Answers

Passing references between game objects 1 Answer

Best way to set game object transforms 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges