Destroy object while instantiate after few seconds

Hi,
I have this code and I want it when the object instantiate it will be destroyed after 1 second and so on with every object

    Vector3 ufoSpeed;
    public float Timer = .1f;
    public Rigidbody ufo;
    void Start()
    {
        ufoSpeed = (new Vector3(0, 0, Random.Range(-5, 5)));

    }


    void Update()
    {
        Timer -= Time.deltaTime;
        if (Timer <= 0)
        {
            Rigidbody ufoClone;
            ufoClone = Instantiate(ufo, new Vector3(0, 5, Random.Range(-5, 5)), transform.rotation) as Rigidbody;
            ufoClone.AddForce(ufoSpeed);
            Timer = 2;
        }
    }

The GameObject.Destroy(); command accepts an optional float type second argument that will delay by that many seconds before destroying something. In the Start() method you could Destroy() the GameObject this script is on after 1 second.

2 Likes