Destroy object after time - Unity C#
Unity Destroy object after time
How to Destroy object after time
Example for destroy a gameObct:
- Pickup a coin when the player collide with the Coin.
- shoot a bullet and destroy The Enemy.
- 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.
Post a Comment
image video quote pre code