Destroy object after time - Unity C#

  Unity Destroy object after time

Destroy object after time - Unity C#

How to  Destroy object after time

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 it collide with the Floor.
Exeple destroy a game object after time, exemple  a Bubble  collide  with the Floor be destroyed  after 2 seconds :

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

public class Bubble MonoBehaviour
{
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.tag == "Floor")
        {
            
            Destroy(gameObject, 2f);
            
        }
    }
    
}

The floor and bulle must have a collider component, in the collider of the Bubble set Trigger case enable.