Destroying instantiated ground after player passes it for a distance

Hello guys, I’m trying to write a ground spawning script for an endless runner. In order to remove the unwanted clones i am trying to destroy the spawned grounds after player passes them for a distance but i could not find a way. The code i’m using right now is this :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GroundSpawn : MonoBehaviour
{
    public GameObject[] obj;
    public Transform startingGround;
    int cloneNum = 0;
    public Transform car;
    Transform cloneDestroy;




    void Start ()
    {
        Spawn ();
    }

    void Spawn()
    {
        cloneNum++;
        Vector3 newPosition =  new Vector3 (cloneNum*55f,0,0);
        var clone = Instantiate (obj [Random.Range (0, obj.Length)], startingGround.position + newPosition, Quaternion.identity);
        Invoke ("Spawn", 1f);
    }

    void Update()
    {
        cloneDestroy = GameObject.Find("Road1(Clone)").gameObject.transform;
              
        if ((car.transform.position.x + 30f) >= cloneDestroy.transform.position.x)
            Destroy (cloneDestroy);
    }

}

The compiler or unity does not gives any errors and instantiates grounds at desired positions but does not destroys them after the player passes them.

Edit:
Also if there is a way to find the length of the prefabs i’m using that would be actually nice. I can use it instead of multiplying the cloneNum with a float in order to instantiate the new ground where the last one ends.

I think you can use GetComponent().bounds for the length.
It might be a good thought/idea to “pool” these rather than destroying them. Think of it as “recycling”… You have an allocation amount (that you either start with, or work up to), but when you had previously wanted to destroy them, simply move older/behind ones ahead to where you would have previously created a new one.
:slight_smile:

But when pooling what will happen to randomness ? I mean for now my only ground prefab is a scaled basic cube :smile: but my friend is working on something. Lets say i have 3 different grounds and i want them to instantiate randomly. Doesn’t this pool loops itself ?

By the way, thanks for .bounds tip

Okay, well depends on the different bits. If they are complex, go for 3 pools maybe.
If it’s something super simple, like a colour (??) i dunno… you could just assign that when taking it out of the pool to (re)-use it. :slight_smile:
The overall moral here would be that you’re keeping in memory what you’re going to be using later anyways, and not having to instantiate it, saving yourself a little performance there. Memory should always be similar, since you would have had them anyways.

np :slight_smile:

Hmm, good point on saving memory. But since i’m new to all this before a search can i ask if this can be match up with the player speed. Since i am thinking of speeding up the player as it moves further and further

Well, sure… Since the area you’d show/have before and after is still a fixed number of objects, even going faster won’t change that, right?

But aren’t we talking about changing the position of the grounds like when player passes them put it end of the current road. So can this replacement speed always catch up with the player speed ?

Well, Dunno how your game works (ie: Camera), if you see behind you or whatever… either you can swap it right away after the player passes it or you can swap it after 3 blocks pass (sorry hard to guess without knowing).
Either way, can the road be moved as fast as the player? :slight_smile:
I mean this in a fun, and polite way:
Ask yourself, can you assign a variable as fast the the car in your game can travel? :stuck_out_tongue:

That’s the problem of being a begginer :stuck_out_tongue: . But thanks for all the help. I’ll try this.