Remove old Objects in scene

Okay. I’m making an infinit runner. But I can’t figure out how to remove objects that I’ve run past.
I am very sorry if my code is bad. This is my first game.
I need a no-lag solution. :slight_smile:

IF you want to see what the game looks like:


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

public class generation : MonoBehaviour {

    private float speed = 5f;
    public static float groundNumber = 10;
    private float passedNumber = 0f;
    GameObject startPlatform;
    GameObject gro

    public float id = 0f;

    public static GameObject wallPrefab;
    public static float genNumber;
    public static GameObject groundPrefab;
    // Use this for initialization
    void Start () {

        startPlatform = GameObject.Find("StartingPlatform");
        wallPrefab = GameObject.Find("Wall");
        groundPrefab = GameObject.Find("StartingPlatform");
        wallPrefab.GetComponent<Renderer>().material.color =;
        groundPrefab.GetComponent<Renderer>().material.color = Color.white;
        groundNumber = 10;
        neverRepeat = false;
        neverRepeat2 = false;
        neverRepeat3 = false;
        neverRepeat4 = false;


    public static bool neverRepeat = false;
    public static bool neverRepeat2 = false;
    public static bool neverRepeat3 = false;
    public static bool neverRepeat4 = false;
    private int wall = 0;
    // Update is called once per frame
    void DestroyPlatform ()

    void FixedUpdate () {

        #region Ground

        GameObject ground = GameObject.CreatePrimitive(PrimitiveType.Cube);
        ground.transform.position = new Vector3(0, -1, 0);
        ground.transform.localScale = new Vector3(10, 1, 10);
            ground.tag = "platformNumber";
        ground.transform.position = new Vector3(0, -1, groundNumber);

        groundNumber += 10;
        if (groundNumber > 1000 && neverRepeat == false)
            Debug.Log("Ground Number is more than 1000");
            neverRepeat = true;
            playerController.movementSpeed += 2f;
            id = +1;


        if (groundNumber > 3000 && neverRepeat2 == false)
            Debug.Log("Ground Number is more than 3000");
            neverRepeat2 = true;
            playerController.sideMoveSpeed += 0.01f;
            playerController.movementSpeed += 2f;
        if (groundNumber > 5000 && neverRepeat3 == false)
            Debug.Log("Ground Number is more than 5000");
            neverRepeat3 = true;
            playerController.movementSpeed += 1f;
        if (groundNumber > 7000 && neverRepeat4 == false)
            Debug.Log("Ground Number is more than 7000");
            neverRepeat4 = true;
            playerController.sideMoveSpeed += 0.01f;
            playerController.movementSpeed += 1f;

        Vector3 wallPOS = new Vector3(Random.Range(-5f, 5f), 0, Random.Range(-5f, 5f) + id);
        Instantiate(wallPrefab, wallPOS, Quaternion.identity);
        id += 10;



Good day @CloudiaNBusiness .

I did not read your code, i will just give you a solution for your problem. As i understand, you have a camera that is moving forward over a platform, and you want to destroy everything behind the camera because you will never see it again.

Than what i recommend you is to create a empty GameObject children & behind the camera. This way, this object will move with the camera. Lets call this object “Destroyer”.

Then add a BoxCollider component to Destroyer, and activate it “Trigger” option. Make this Collider big enough to collide with all objects that are in the platform, including the platform. As it will move with the camera, all objects once get out of camera vision, have to collide with the Destroyer.

We will make a script that destroys all the GameObjects that touches the Destroyer. For do this we will use the “OnTriggerEnter” wich will be called when some other collider touches the Destroyer collider. So, All GameObjects that you want to be destroyed, must have a BoxCollider!

So, the Destroyer must have this code in one of its scripts:

private void OnTriggerEnter (Collider ColliderToBeDestroyed)

Note that “ColliderToBeDestroyed” is a Collider component, so you have to refer to the GameObject that contains that collider that has touched the Destroyer

Well, i think i said all you need to do this. I recommend you to make segments of the platform to be able to destroy the platform either.

If need something else, just ask using @tormentoarmagedoom