• 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 Zyrac · Sep 01, 2014 at 03:15 PM · collisiondestroyrigidbody2d

Destroy rigidbody2D after collision?

Hi, in my 2D game, I have bombs that try to drop on the player. However, I've hit a bit of a problem. When the bombs explode, if the player has the shield activated, I want them to be able to pass through the explosion. (I have a 2D animation for the explosion). For this, I believe I need to remove the 2D collider and 2D rigidbody. Here's where the problem is. Here is a bit of the code with the part regarding the collisions and explosion:

 IEnumerator explosion()
     {
         yield return new WaitForSeconds(1);
         Destroy(this.gameObject);
     }
 
     void OnCollisionEnter2D(Collision2D collision)
     {
         if(collision.gameObject.name == "cube")
         {
             animator.SetInteger("Bomb_State", 1);
             bombexploded = true;
             rigidbody2D.isKinematic = true;
             Destroy(this.gameObject.collider2D);
             Destroy(this.gameObject.rigidbody2D);
             StartCoroutine(explosion());
         }
 
         if(collision.gameObject.name == "metaball")
         {
             animator.SetInteger("Bomb_State", 1);
             bombexploded = true;
             rigidbody2D.isKinematic = true;
             Destroy(this.gameObject.collider2D);
             Destroy(this.gameObject.rigidbody2D);
             StartCoroutine(explosion());
         }
     }

When I try this code, however, Unity crashes. It does not crash if I remove the "Destroy(this.gameObject.rigidbody2D);" but this then does not do what I want it to do. My guess is that the problem lies in trying to destroy the 2D rigidbody in OnCollisionEnter2D, as OnCollisionEnter2D depends on the 2D rigidbody. But surely there must be some way around it? Thanks.

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 Scribe · Sep 01, 2014 at 03:27 PM 0
Share

Perhaps try simply deactivating them rather than destroying them?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ali-hatem · Sep 02, 2014 at 12:46 PM

hi: 1- use tags not names like this : if(collision.gameObject.tag == "cube") note you have to tag your objects in unity and tag it the same as in the script for example your tag in unity must be cube .

2- you don't need destroy (this.gameobject) and Destroy(this.gameObject.collider2D) and Destroy(this.gameObject.rigidbody2D) just use destroy(gameobject) and to destroy other gameobject use destroy(collision.gameobjects).

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 Zyrac · Sep 02, 2014 at 01:45 PM 0
Share

Well, guys, thanks for all your help. I seem to have fixed it! :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

25 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

Related Questions

OnCollisionEnter2D is not detecting collisions. 2 Answers

Bullets wont collide and destroy object 1 Answer

Respawn when a character hits an object with a tag of "Kill" , but after 3 deaths the controller is destroyed 0 Answers

Help with collisions and destroy please 1 Answer

OnTriggerEnter - destroy (this.gameobject) if it collides with anything 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