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
0
Question by ashjack · Dec 08, 2013 at 08:36 PM · c#collisionrigidbodycomponentdelete

Deleting a rigidbody

I need to delete a rigidbody for my group to function. I have dragable parts that join into a group on collision. But the parts still move separately unless I manually delete their rigidbodies. But how do I do this through a c# script? I have tried this:

 public class ObjectAttach : MonoBehaviour {
 
 public Transform Group1;
     
     
     void Start()
     {
         
         Group1 = GameObject.FindWithTag("Group").transform;
     }    
     
     
     void OnCollisionEnter(Collision collision)
     {
           if(collision.gameObject.tag=="Part")
         {
             
             collision.transform.parent = Group1;
             GameObject.FindGameObjectsWithTag("Part").Destroy(Rigidbody);    
         }  
     }
 
 }

But that gives me this error:

Assets/Resources/ObjectAttach.cs(23,67): error CS1061: Type UnityEngine.GameObject[]' does not contain a definition for Destroy' and no extension method Destroy' of type UnityEngine.GameObject[]' could be found (are you missing a using directive or an assembly reference?)

What do I do?

Comment
Add comment · Show 4
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 meat5000 ♦ · Dec 08, 2013 at 08:39 PM 0
Share

Would making them $$anonymous$$inematic have the same effect you are after?

avatar image robertbu · Dec 08, 2013 at 09:09 PM 0
Share

As an alternate to deleting the rigidbody, you could connect the rigidbodies together using a FixedJoint.

avatar image Roland1234 · Dec 08, 2013 at 09:22 PM 0
Share

@robertbu - Good call, except that in this case that would require maintaining a chain of FixedJoints linking all objects in the group as he is grouping them by a single Transform that for all we know doesn't belong to a single object with a Rigidbody they could all join to.

avatar image meat5000 ♦ · Dec 08, 2013 at 09:26 PM 0
Share

....or parent them all to an another object, so you can just move that one?

1 Reply

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

Answer by Roland1234 · Dec 08, 2013 at 09:05 PM

You are trying to call .Destroy off of what FindGameObjectsWithTag returns, which is an array of GameObjects, not a singular GameObject. Based on the description of your problem, if you want to remove the Rigidbody component of the thing that you've collided with you would do the following:

 void OnCollisionEnter(Collision collision)
 {
     if(collision.gameObject.tag=="Part")
     { 
         collision.transform.parent = Group1;
         if(collision.rigidbody != null)
         {
             Destroy(collision.rigidbody);
         }
     }
 }

Although I world agree with meat5000's comment and suggest that making the rigidbody kinematic may be more appropriate.

Comment
Add comment · Show 2 · 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 ashjack · Dec 09, 2013 at 04:35 PM 0
Share

Thanks for your answer. I can not make the rigidbody kinematic, as I have a script that makes them kinematic whenever they are not being dragged, so that they don't fly off if hit.

avatar image Roland1234 · Dec 10, 2013 at 02:23 AM 0
Share

You would know best of course about the specific requirements for your project, but as far as the original question goes (how to delete a Rigidbody component in a script) please accept my answer as it does answer your original question. Gotta rack them points up, you know? ;)

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

20 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

Related Questions

Any way to ignore collision between rigidbodies and colliders/character controllers? 1 Answer

Freeze Gameobject's position on collision 0 Answers

Rigidbody necessary to use collision detection 0 Answers

How to hit gameobjects to go into the opposite direction of hit? 1 Answer

Why can't my enemies walk up an incline? 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