Destroying the instantiated objects ?

Hi,

I am instantiating objects on x axis the problem is I want to destroy them after sometime I didn’t know how, I tried some ways but it seemed it didn’t work, this code I am using:

function spawn() : IEnumerator {
	for (var i = 0; i < max; i++) {
        yield WaitForSeconds(0.05);
        var theNewPos= new Vector3 (xStartingPos,Random.Range(minHeight,maxHeight),35);
        var go : GameObject = Instantiate(theObject);
        go.transform.position = theNewPos;
        
        }
}

Function DestroyTheObject ()
{
yield waitforseconds (30);//or however long you want
Destroy(go);
}

Then call this function after you instantiate the object. I don’t know unity script well so there could be some syntax errors here. Basically, use an ienumerator .

You can specify a time parameter to Destory(). So if you want it destroyed 3 seconds from now, you can call:

Destroy(go, 3.0);