For this task, we can take the skills we've already learned and make the bullets responsible for their self-destructive behavior, as follows:
- Create a new C# script in the Scripts folder and name it BulletBehavior.
- Drag and drop the BulletBehavior script onto the Bullet prefab in the Prefabs folder and add the following code:
public class BulletBehavior : MonoBehaviour
{
// 1
public float onscreenDelay = 3f;
void Start ()
{
// 2
Destroy(this.gameObject, onscreenDelay);
}
}
Let's break down this code, as follows:
- We declare a float variable to store how long we want the Bullet prefabs to remain in the scene after they are instantiated.
- We use the Destroy() method to delete the GameObject.
- Destroy() always needs an object as a parameter. In this case, we use this keyword to specify the object that the script...