How to Destroy a GameObject on Collision in Unity

How to Destroy a GameObject on Collision in Unity

How to destroy a gameObject with collision in Unity


Example for destroy a gameObct:

  1. Pickup a coin  when the player collide with the Coin.
  2. shoot a bullet and destroy The Enemy.
  3. Destroy a gameObgect when itcollide with the Floor.

Exeple for Player pickup the Coin :


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CurrencyPickup : MonoBehaviour
{
    }
    private void OnTriggerEnter2D(Collider2D other)
    {
        if(other.gameObject.tag == "Coin")
        {
            
            Destroy(other.gameObject);
            
        }
    }
    
}

The player and coin must have a collider component, in the collider of the coin set Trigger case enable.


Tag: Destroy prefab unity,Unity destroy and spawn object, Unity destroy object on collision,Unity movement script,How to destroy another object in Unity,How to Instantiate a GameObject Unity,Unity Destroy object after time