Day 4 Solutions




Spawner

 

using UnityEngine;

public class Spawner : MonoBehaviour
{
    public GameObject prefab;
    float timer;

    // Update is called once per frame
    void Update()
    {

        if (timer < 0)
        {
            SpawnPrefab();
            timer = 1f;
       }

       timer -= Time.deltaTime;
    }

    void SpawnPrefab()
    {
        //make a copy of the prefab, and store it in a variable
        GameObject newObject = Instantiate(prefab);

        //move the copy to match the spawner's position
        newObject.transform.position = transform.position;
    }
}


ProjectileLifespan


using UnityEngine;

public class ProjectileLifespan : MonoBehaviour
{
    public float lifespan = 3f;


    // Update is called once per frame
    void Update()
    {
        lifespan -= Time.deltaTime;

        if (lifespan <= 0)
        {
            Destroy(gameObject);
        }
    }
}