Block Spawning and deleting

Hi guys, so I’m essentially trying to create a kind of infinite runner, and part of it for me is to spawn just a block continuously, with random spacings apart.
Of course, this will get laggy, so I delete them after a while.
I put the following script on a block, save it as a prefab, and add the prefab as the Obj

#pragma strict
var Obj : GameObject;

function Start () {
var RanDistance = Random.Range(8,21);

var Dist = transform.position;
Dist.z += RanDistance;

yield WaitForSeconds (0.2);
Instantiate(Obj, Dist, transform.rotation); 
yield WaitForSeconds (10);
Destroy (gameObject);

}

function Update () {


}

This works, however, eventually, the deleting catches up too much with the spawning, and increasing the wait time before deleting only delays the time until that happens. Any fix for what I want to do?

Sounds like your instantiation loop is running at a different speed to your player. One way to fix it is to Instantiate and Destroy based on distance, instead of time.

Note: In general infinite runners make heavy use of Object pooling. Instantiate and Destroy tend to become very expensive after a while. GC and all.

If I read your question correctly, then I think all you have to do is destroy the block a couple seconds after the player touches it. Thant should fix your problem, right? Instead of putting your code in function start, put it in an onCollisionEnter function.