1. Submit bugs to enter our 5.4 Beta Sweepstakes. Read more about it here.
  2. Unity 5.4 public beta is now available for all users. Grab the build and visit the 5.4 beta forum.
  3. Missing a module? More about the new installer in 5.3 here.
  4. 4.7.1 released for Xcode 7.3 support. Thread here.

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:
    584
    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:
    584
    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.