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 $$anonymous$$ · Jun 19, 2017 at 11:56 PM · rigidbody2dcollider2dnot workingnoobcollision2d

[SOLVED] How to Destroy a rigidbody game object on collision with another gameobject?

PS: Answered in best answer below! check it out! :) feel free to PM me on discord: Khaled#1130 if you have any questions, I might just be able to help!


Hello!

I am sure this is answered some were but I have searched for too long.. still am and still suck at solving the problem. I am an utter noob but oh well, did not find an answer to my specific case so i am hoping one of you loves would be able to help me with it.

My project is in 2D.. a rigid body 2D ball that moves by forces controlled by the player would not get destroyed in contact with another object (laser) The ball has a circle collider 2D, a rigidbody2D and a script that controls movement and forces The laser has a boxcollider 2D and a script that controls the death of the player

The script on the laser is (other is refered the Player)

 using UnityEngine;
 using System.Collections;
 
 public class DestroyPlayer : MonoBehaviour
 {
     public GameObject other;
 
     void OnTriggerEnter2D(Collider2D other) 
         {
             Destroy(other);
         }
 }

I have tried numerous other scripts and kept trying different edits and it just would not work. Thank you!

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer Wiki

Answer by $$anonymous$$ · Jul 04, 2017 at 07:02 PM

Hello @RadiationAlert and @altaiirdesmond and @RhisisLS

First iam extremely sorry I could not reply earlier because my router was really crappy. ..... ........

I tried your method @altaiirdesmond but sadly it did not work and only made a compiler error, I belive it was because its gameObject instead of gameobject, anyway I happened to have a friend over discord who happened to know alot about these kind of stuff, all he did really was view some documentations and voila! he gave me the code that actually worked!

.......

You attach this to the player not the laser object, also create a tag for your Player called "Player" (cap sensitive) and asign it to your player.

There is the script

 using UnityEngine;
 using System.Collections;
 
 public class DestroyPlayer : MonoBehaviour {
     void OnCollisionEnter2D(Collision2D coll) {
         if (coll.gameObject.tag == "Player") //Change tag
             Destroy(coll.gameObject);
     }
 }


My concern however is why did not the other scripts work? They all practicely say the same thing.

PS: (the name (DestroyPlayer) in the script above will depend on what you name your script by, do not copy and paste this unless you have named your script DestroyPlayer exactly (cap sensitve) or edit it to your script's name. Other wise you will recive an error but this is easily fixed.

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
1

Answer by RadiationAlert · Jun 20, 2017 at 03:07 AM

@Khaled125 Well from the looks of it, your colliders might not be set to triggers? Otherwise, altaiirdesmond's solution should work correctly.

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 altaiirdesmond · Jun 20, 2017 at 12:27 AM

@Khaled125

create a tag "Laser" then set the tag of the laser gameobject to Laser . Attach this script to the player

  public class DestroyPlayer : MonoBehaviour
  {
       //I have removed the "public gameobject other" since I'm assuming that this script is 
       //attached to the player.
       void OnTriggerEnter2D(Collider2D other) 
          {
              //if the player collider hits a gameobject with tag "Laser"
              //then the gameobject the script is attached to will be destroyed
              if(other.gameobject.tag == "Laser")
                          Destroy(other);
          }
  }


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 Hamilcar-games · Jul 04, 2017 at 09:26 PM

void onCollisionEnter() { destroy(gameobject); }

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

74 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Player not taking damage on collision with enemy 1 Answer

2d rigidbody falling from the collider when it is moved 3 Answers

Disable click action when collision 1 Answer

Fixing an inconsistent 2D collision 0 Answers

multiple objects on oncollissionstay2d 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