- Home /
 
If gameobject on collision with gameobject then destroy
I want to make it so when I attack and the attackTrigger collision thing is on collision with a watermelon for example, it will destroy that watermelon. My attackTrigger appears for 0.2 seconds when I attack but when it does appear, I want what gameobject it is on collision with, to be destroyed. My attackTrigger box collider has a is trigger, and the fruit does not have a is trigger. My character has a rigid body. This script doesn't work, when it is on collision, it doesn't destroy the fruit. How can I make this work script work and destroy the gameobject? Thank you in advance.
 using UnityEngine;
 using System.Collections;
 
 public class attackTrigger : MonoBehaviour {
 
     public GameObject watermelon;
     public GameObject strawberry;
     public GameObject grape;
     public GameObject banana;
     public GameObject orange;
     public GameObject tomato;
     public GameObject pineapple;
     public GameObject apple;
     public GameObject blueberries;
     public GameObject raspberries;
     public GameObject pear;
     public GameObject blackberries;
     public GameObject lemon;
 
     void OnTriggerEnter2D(Collider2D col)
     {
 
         if(col.isTrigger != true && col.gameObject == watermelon)
         {
             Destroy (watermelon);
         }
         if(col.isTrigger != true && col.gameObject == strawberry)
         {
             Destroy (strawberry);
         }
         if(col.isTrigger != true && col.gameObject == grape)
         {
             Destroy (grape);
         }
         if(col.isTrigger != true && col.gameObject == banana)
         {
             Destroy (banana);
         }
         if(col.isTrigger != true && col.gameObject == orange)
         {
             Destroy (orange);
         }
         if(col.isTrigger != true && col.gameObject == tomato)
         {
             Destroy (tomato);
         }
         if(col.isTrigger != true && col.gameObject == pineapple)
         {
             Destroy (pineapple);
         }
         if(col.isTrigger != true && col.gameObject == apple)
         {
             Destroy (apple);
         }
         if(col.isTrigger != true && col.gameObject == blueberries)
         {
             Destroy (blueberries);
         }
         if(col.isTrigger != true && col.gameObject == raspberries)
         {
             Destroy (raspberries);
         }
         if(col.isTrigger != true && col.gameObject == pear)
         {
             Destroy (pear);
         }
         if(col.isTrigger != true && col.gameObject == blackberries)
         {
             Destroy (blackberries);
         }
         if(col.isTrigger != true && col.gameObject == lemon)
         {
             Destroy (lemon);
         }
 
     }
 
 }
 
 
              It is hitting the fruit for sure but it is not sending any messages.
If it is a collision, you want to make sure to use OnCollisionEnter2D and Collision2D ins$$anonymous$$d of OnColliderEnter2D
That sadly didn't work, but I made it work a different way, but I want to check gameobject rather than tag. Could I somehow fix this?
 using UnityEngine;
 using System.Collections;
 
 public class attackTrigger : $$anonymous$$onoBehaviour {
     
     public GameObject watermelon;
     public GameObject strawberry;
     public GameObject grape;
     public GameObject banana;
     public GameObject orange;
     public GameObject tomato;
     public GameObject pineapple;
     public GameObject apple;
     public GameObject blueberries;
     public GameObject raspberries;
     public GameObject pear;
     public GameObject blackberries;
     public GameObject lemon;
     
     void OnTriggerEnter2D(Collider2D col)
     {
         
         if(col.isTrigger != true && col.CompareTag("Fruit"))
         {
             Destroy (watermelon);
             Debug.Log("Hello");
         }
                 In your old script you checked the gameobject ins$$anonymous$$d of tag...
If you want to destroy the gameObject you are colliding with then you should probably just destroy the gameObject you are colliding with. i.e.
  void OnTriggerEnter2D(Collider2D col) {         
      if(col.isTrigger != true) {
           Destroy(col.gameObject);
      }
  }
 
                 Your answer