code on the bullet:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public int secondsBeforeDestroy;
public void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Enemy")
{
Destroy(collision.gameObject);
Debug.Log("Collision");
}
}
void Start()
{
StartCoroutine(DestroyBullet());
}
IEnumerator DestroyBullet()
{
//yield on a new YieldInstruction that waits for secondsBeforeDestroy seconds.
yield return new WaitForSeconds(secondsBeforeDestroy);
//After we have waited secondsBeforeDestroy seconds Destroy This GameObject print the time again.
Destroy(gameObject);
Debug.LogWarning("Destroyed Bullet");
}
}