Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

How would i destroy self after collision?

Discussion in 'Physics' started by mcarthur03, Sep 6, 2015.

  1. mcarthur03

    mcarthur03

    Joined:
    Jan 29, 2015
    Posts:
    26
    I have tried many different strategies which have all failed, could someone please make a simple script that destroys the object it is attached to when it collides with ANY other object (not a specific object)
     
  2. Cloaking_Ocean

    Cloaking_Ocean

    Joined:
    Jul 5, 2015
    Posts:
    4
    Code (CSharp):
    1. void OnTriggerEnter (Collider other) {
    2.     Destroy(gameObject);
    3. }
    Code (JavaScript):
    1. function OnTriggerEnter (Collider other) {
    2.     Destroy(gameObject);
    3. }
    I think that works? Not sure. Will test when I can.
    Make sure you spell everything right XD I remeber when I spelt Destroy Destory
     
    Last edited: Sep 7, 2015
  3. mcarthur03

    mcarthur03

    Joined:
    Jan 29, 2015
    Posts:
    26
    Didn't work, anything else you may have missed?
     
  4. Cloaking_Ocean

    Cloaking_Ocean

    Joined:
    Jul 5, 2015
    Posts:
    4
    So the way the C# code I gave you before works is you need to have some object have "Is Trigger" Enabled on it's Collider. then when it goes through the object it will delete it's self. If you don't want to add Is Trigger to everything here's a different method.

    Code (CSharp):
    1. void OnCollisionEnter(Collision other) {
    2.         Destroy(gameObject);
    3. }
     
  5. mcarthur03

    mcarthur03

    Joined:
    Jan 29, 2015
    Posts:
    26
    neither of them worked
    I have a 2D bullet, it has a collider which is a trigger, it also has the exact c# code you gave me, not working...
     
  6. Bolchev

    Bolchev

    Joined:
    Aug 3, 2015
    Posts:
    16
    well then if it is 2D I think the proper code is :
    Code (csharp):
    1.  
    2. void OnTriggerEnter2D (Collider2D other)
    3. {
    4. Destroy (gameObject)
    5. }
    6.  
    give it a try
     
  7. mcarthur03

    mcarthur03

    Joined:
    Jan 29, 2015
    Posts:
    26
    sorry I couldn't reply, the unity forums refused to work, testing it out now!
     
    Last edited: Sep 16, 2015
  8. mcarthur03

    mcarthur03

    Joined:
    Jan 29, 2015
    Posts:
    26
    it works, now how would I make it collide with only, lets say 2 different objects out of 4?
     
  9. mcarthur03

    mcarthur03

    Joined:
    Jan 29, 2015
    Posts:
    26
    works! how would I make it so it collides with everything except for one thing now?
     
  10. BitBanga

    BitBanga

    Joined:
    Apr 20, 2015
    Posts:
    5