How do I destroy past obstacles in an endless runner?

I have obstacles spawning ahead of the runner and want them to be destroyed after the runner passes them. Here is the instantiation code attached to an empty game object:

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

public class Spawner : MonoBehaviour
{

    public GameObject[] obstaclePatterns;
    public Transform player;
    private float timeBtwSpawn;
    public float startTimeBtwSpawn;
    public float obstacleSpawnDistance;
    public float obstacleSpDMin;
    public float obstacleSpDMax;
    public float decreaseTime;
    public float minTime = 0.65f;
    public float obstacleHeightMin = 10f;
    public float obstacleHeightMax = 15f;

// makes obstacles randomly ahead of player

    private void FixedUpdate() {
        if (timeBtwSpawn <= 0) {
            obstacleSpawnDistance = Random.Range(obstacleSpDMin, obstacleSpDMax);
            int rand = Random.Range(0, obstaclePatterns.Length);
            Instantiate(obstaclePatterns[rand], new Vector3(obstaclePatterns[rand].transform.position.x, Random.Range(obstacleHeightMin, obstacleHeightMax), player.position.z + obstacleSpawnDistance), Quaternion.identity);
            rand = Random.Range(0, obstaclePatterns.Length);
            Instantiate(obstaclePatterns[rand], new Vector3(obstaclePatterns[rand].transform.position.x, Random.Range(obstacleHeightMin, obstacleHeightMax), player.position.z + obstacleSpawnDistance), Quaternion.identity);
            rand = Random.Range(0, obstaclePatterns.Length);
            Instantiate(obstaclePatterns[rand], new Vector3(obstaclePatterns[rand].transform.position.x, Random.Range(obstacleHeightMin, obstacleHeightMax), player.position.z + obstacleSpawnDistance), Quaternion.identity);
            timeBtwSpawn = startTimeBtwSpawn;
            if (startTimeBtwSpawn > minTime) {
                startTimeBtwSpawn -= decreaseTime;
            }
        }
        else {
            timeBtwSpawn -= Time.deltaTime; 
        }
    }
}

And here is the destroy code attached to my obstacle prefab:

using UnityEngine;

public class ObjectDestruction : MonoBehaviour {

    public Transform player;

    // Update is called once per frame
    void FixedUpdate()
    {
        if (transform.position.z < player.position.z) {
            Destroy(gameObject);
            Debug.Log("test");
        }
    }
}

Thank you so much in advance!!

Hi! There’s a easier way! You just have to destroy the object after a number of seconds.If it’s a endless runner i think 5 sec it’s actually enough.Just atach this script to your enemy prefab or whatever you use.164678-captura-ecran-66.png