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
4
Question by KravenArk · Oct 06, 2015 at 10:06 PM · collision detectioncollision event

Destroy OTHER objects on Collision Unity

I found a BUNCH of questions and comments about destroying an object if it is hit by something but in my scene, I want the PROJECTILE to be destroyed. How do I make it that anything that collides with an object gets destroyed? It's sorta like a reverse bullet. c# please

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
7

Answer by Cherno · Oct 06, 2015 at 10:18 PM

For a collision to register, the following needs to be true:

  1. The objects needs a non-kinematic rigidbody and a non-trigger collider.

  2. The other object needs a non-trigger collider.

  3. One of the colliders needs to be convex.

  4. Both objects needs to be in layers that are set up to register collision (project settings -> physics).

For a Trigger Collision to register, the other object needs a convex collider set to IsTrigger = true. Note that Addyarb posted an example that uses OnTriggerEnter. For a normal collision, you would use OnCollisionEnter():

 void OnCollisionEnter(Collision collision){
          Destroy(collision.collider.gameObject);
          Destroy(gameObject);
          }
 
 

Bonus tip: Insert Debug.Log lines to see which parts of your code are called.

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
5

Answer by Addyarb · Oct 06, 2015 at 10:08 PM

Here's both ways to destroy the gameObject, or the gameObject you collide with.

   void OnTriggerEnter(Collider col){
         Destroy(col.gameObject);
         }
     
     void OnTriggerEnter(){
     Destroy(gameObject);
     }




Comment
Add comment · Show 4 · 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 KravenArk · Oct 06, 2015 at 10:10 PM 0
Share

So in the second one, it destroys the object that is hit, right? And in the first it destroys the objects that hits it, right? Or do I have them mixed up? I'll try them both. Thank You

avatar image KravenArk · Oct 06, 2015 at 10:12 PM 0
Share

uh oh...Nothing seems to be happening at all. Neither of them are being destroyed. Hmm...The projectile has a rigid body but the object to be hit doesn't. Is that a problem?

avatar image Addyarb · Oct 06, 2015 at 10:20 PM 0
Share

Yeah try 'em out!

Actually the parameter that you're supplying in the OnTriggerEnter method is the object you're hitting. You can call it whatever you want to make it easier. For instance:

 void OnTriggerEnter(Collider TheColliderThatIWillBeCollidingWith){
      if(col.tag == "Target"){
           Destroy(TheColliderThatIWillBeCollidingWith.gameObject);
      }
 }


You'll need to put a collider with "is Trigger" enabled as well for OnTriggerEnter :)

avatar image Addyarb · Oct 06, 2015 at 10:23 PM 0
Share

Also as Cherno said - you'll need to have one object with a non-trigger collider, and one with. In this case - you are calling OnTriggerEnter(Collider col) which basically means you are checking THIS object's script for a collision on its trigger collider - and supplying the OnTriggerEnter method with a parameter which is the object that you collided with.

In fact, it's essentially setting a temporary reference to the object you collided with - so you can even access components, transforms, tags, etc. once you collide with it.

i.e.

 col.GetComponent<Script>().scriptIntVariable = 10;
avatar image
2

Answer by neevcuber · Jun 08, 2018 at 01:56 AM

to make the OTHER destroying stuff you told copy this code:

 public Transform gameobject;
 void OnCollisionEnter(Collision coll) {
 if (coll.gameobject.name == mycube) {
 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

32 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

Related Questions

Strange Collisions (...as there should be none!) 0 Answers

Specify collision notification order? 0 Answers

any way to get a pre-solve collision callback? 3 Answers

OnCollisionExit2D workaround? 0 Answers

OnColliderEnter2D not working 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