Search Unity

  1. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  2. Unity 2017.2 is now released.
    Dismiss Notice
  3. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  6. Unity 2017.3 beta is now available for download.
    Dismiss Notice

destroy on collision

Discussion in 'Editor & General Support' started by pat_sommer, Jul 20, 2010.

  1. pat_sommer

    pat_sommer

    Joined:
    Jun 28, 2010
    Posts:
    586
    hey everyone.. im having some issues with getting my missile to be destroyed on its collision..

    Code (csharp):
    1. function OnCollisionEnter  (collisionInfo : Collision);
    2.  
    3. if (collisionInfo.gameObject.tag == ("block")
    4. {
    5. Destroy(gameObject);
    6. }
    thats the script i have attached to my missile, "block" is the tag of an item it will collide with


    any help?
     
  2. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
  3. pat_sommer

    pat_sommer

    Joined:
    Jun 28, 2010
    Posts:
    586
    thanks i actually just figured it out tho... i was making it way more complex then it had to be... simple enuff put this code on the missile prefab

    Code (csharp):
    1. function OnCollisionEnter () {
    2.  
    3. Destroy (gameObject);
    4.  
    5. }
    6.  
    7.  
    8.  
    works like a charm :D
     
  4. BrettFromLA

    BrettFromLA

    Joined:
    Jan 29, 2009
    Posts:
    244
    You rewrote your script, right? I don't think it would run that way. Here's what I expect will run:

    Code (csharp):
    1.  
    2. function OnCollisionEnter  (collisionInfo : Collision) {
    3.     if (collisionInfo.gameObject.tag == ("block")
    4.     {
    5.         Destroy(gameObject);
    6.     }
    7. }
    8.  
    And just to check that everything is triggering when it ought to, try this:

    Code (csharp):
    1.  
    2. function OnCollisionEnter  (collisionInfo : Collision) {
    3.     Debug.Log("collision occurred");
    4.     Debug.Log("tag = " + collisionInfo.gameObject.tag);
    5.     if (collisionInfo.gameObject.tag == ("block")
    6.     {
    7.         Destroy(gameObject);
    8.     }
    9. }
    10.  
    Also, check the "collision detection matrix" (on the page AkilaeTribe referenced) to make sure the missile object type can bump into the block object type.
     
  5. BrettFromLA

    BrettFromLA

    Joined:
    Jan 29, 2009
    Posts:
    244
    Aw man, I was writing while you were figuring it out. Never mind.