Search Unity

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.