Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Wolfrik_Creations · Jun 14, 2016 at 01:20 AM · destroycomponentcomponents

Destroy all scripts on object.

Is there any way I can destroy all components on an object, but still keeping the Transform and the GameObject itself?

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
1
Best Answer

Answer by tanoshimi · Jun 14, 2016 at 07:03 AM

 GetComponents(typeof(Component));

Will give you an array of all components on the object. Iterate over the array and call Destroy () on each item.

Comment
Add comment · Show 1 · 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 Wolfrik_Creations · Jun 14, 2016 at 07:28 AM 0
Share

Thank you! I will test this out in a bit.

This should work perfectly, I didn't know that you could use typeof(Component).

avatar image
0

Answer by Dibbie · Jun 14, 2016 at 01:49 AM

You could use the Destroy and GetComponent method.

Destroy(this.gameObject.GetComponent<Script1OnThisObject>()); would then destroy "Script1OnThisObject" that is on the current game object - assuming that script actually exists, that is.

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 Wolfrik_Creations · Jun 14, 2016 at 06:52 AM 0
Share

Thank you for your reply, Dibbie.

I know how to delete a certain component, or even all of a certain component. But there are over 20 different components (most inactive) that I need all destroyed if the NetworkView isn't $$anonymous$$e.

Basically all components except for the GameObject itself and the Transform.

avatar image Dibbie Wolfrik_Creations · Jun 14, 2016 at 07:09 AM 1
Share

Ah I see - you could try a for-loop then

  //Untested - C#
     private List<Component> componentList = this.GameObject.GetComponents(Component); //this would be a global declaration
 
 for(int i = 0; i < componentList.Count() - 1; i++){
 if(componentList[i] != typeOf(Transform)){
     Destroy(componentList[i]);
     }
 }

You could also remove all the scripts on your prefab at design time, then if the object is supposed to be yours, re-add each one by code, or alternatively, "disable" every component that should not be running, then you can use the same idea above, except check if the component is enabled ins$$anonymous$$d of checking if the type of Transform. The above method will only keep the Transform on that object.

avatar image Wolfrik_Creations Dibbie · Jun 14, 2016 at 07:29 AM 0
Share

Thank you!

this part: if(componentList[i] != typeOf(Transform)){ plus tanoshimi's answer should work perfectly!

avatar image
0

Answer by wuxingogo · Jun 14, 2016 at 09:39 PM

This code can Destroy all component.But the component that depends on other Component don't work.Sorry my english.

 public void DestroyAllComponent1( GameObject go )
 {
     var components = GetComponents<Component>();
     foreach( var t in components )
     {
         if( t is Transform )
             continue;
         Destroy( t );
     }
 }
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 Wolfrik_Creations · Jun 14, 2016 at 10:23 PM 0
Share

Thank you, but I've already got an answer and it worked perfectly.

It sped my game up by a good 10-15FPS on my friends' old computers when we're playing multiplayer.

Also, what is up with the fact that this looks like both C# and JS?

avatar image wuxingogo Wolfrik_Creations · Jun 15, 2016 at 02:54 AM 0
Share

It's csharp code .Then you can have a try.The grammar perhaps looks like js.

avatar image Wolfrik_Creations wuxingogo · Jun 15, 2016 at 03:31 AM 0
Share

Yes, but "var" is JS.

I use JS, BTW.

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

48 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

Related Questions

Destroy specific joints 1 Answer

destroy hinge joint component from another object in javascript 2 Answers

Help removing an AudioSource component from another script 0 Answers

How to safely destroy component with dependants? 2 Answers

Destroying a Transform Component 1 Answer


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