Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by K B · Feb 06, 2011 at 06:57 PM · destroygetcomponentremovefixedjoint

How to Remove Multiple Components of Same Type from Game Object?

I have a gameObject with multiple FixedJoints attached to it. I can't figure out how to remove these components from the gameObject. I read that you are supposed to use Destroy(Object o), but I wasn't sure how to do that with multiple components.

Comment
Add comment · Show 1
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 yoyo · Feb 06, 2011 at 08:32 PM 0
Share

I'm upvoting you for enlightening me to the fact that one game object can have multiple components of the same type, for some reason I thought you couldn't. Thanks!

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by parahunter · Feb 07, 2011 at 09:38 PM

Okay I got this piece of code to work:

    HingeJoint[] joints = GetComponents<HingeJoint>();
    foreach(HingeJoint joint in joints)
        Destroy(joint);

What you do wrong is that you cast the HingeJoints to Component when you store them but don't delete them as HingeJoint when you use destroy on them. I was not sure of this myself but I got this piece of code to work that sorta resembles your code (except I use a generic version of GetComponents which in my experience has proven to be the most predictable and stable way to use GetComponent and GetComponents):

    Component[] joints = GetComponents<HingeJoint>() as Component[];
    foreach(Component joint in joints)
        Destroy(joint as HingeJoint);

This code also works :-)

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 K B · Feb 08, 2011 at 11:41 AM 0
Share

Thanks! That worked. :D

avatar image
1

Answer by parahunter · Feb 06, 2011 at 07:03 PM

You have to use the GetComponents method, loop trough them and then call Destroy() on all of the components that are returned from GetComponents. It's page on the documentation explains how you use it and loop trough the found components afterwards.

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 K B · Feb 06, 2011 at 07:53 PM 0
Share

Thanks, but this doesn't seem to work. I printed out the length of the array returned by GetComponents before and after I Destroyed each component, and the all of the components were still there.

avatar image parahunter · Feb 06, 2011 at 08:12 PM 0
Share

Then you probably called Destroy the wrong way. try to post your code here and I'll take a look at it

avatar image K B · Feb 06, 2011 at 11:17 PM 0
Share

Component[] joints = myGameObject.GetComponents(typeof(FixedJoint)) as Component[]; for(FixedJoint j in joints) Destroy(j);

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

No one has followed this question yet.

Related Questions

Checking if a component has been removed or not at runtime 1 Answer

destroy doesn't remove the graphics 1 Answer

How to remove an object from an array once it has been destroyed 1 Answer

how do I remove a class item from a list? 1 Answer

Move object, then destroy it. 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