1. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  2. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  3. All Unity Pro perpetual license customers: your special offer to subscribe is ready! Click here.
    Dismiss Notice
  4. Unity 5.5 is now released.
    Dismiss Notice
  5. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  6. Unity 5.6 beta is now available for download.
    Dismiss Notice
  7. Get prepared for the Tizen Mobile App Incentive Program! Read more about the upcoming program here.
    Dismiss Notice
  8. Enter the Microsoft Developer Challenge for a chance to win prizes. Read more about it here.
    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
    Messages:
    585
    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
    Messages:
    1,149
  3. pat_sommer

    pat_sommer

    Joined:
    Jun 28, 2010
    Messages:
    585
    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
    Messages:
    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
    Messages:
    244
    Aw man, I was writing while you were figuring it out. Never mind.