How to randomly generate obstacles on a straight plane and destroy them once passed

Hi,
I want to make it so that obstacle randomly generate with some distance. And automatically destroy themselves one a certain number of objects are spawned. Like when 15 objects are spawned then start deleting from the 1st object spawned.
Pls Help!

What have you tried? What’s not working?

I tried creating 3 objects and randomly spawning it but it seems like the random object spwans evry frame and it doe not destroy.

Well share your code and let’s start working on it together.

Thanks buddy! I will send it in like 15 min as I am not on my setup.

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

public class obstacleSpawner : MonoBehaviour
{
    public GameObject[] obstaclePrefab;
    public float spawnTime = 1;
    private float timer = 0;

    void Update()
    {
        if (timer > spawnTime)
        {
            int rand = Random.Range(0, obstaclePrefab.Length);

            GameObject obs = Instantiate(obstaclePrefab[rand]);
            obs.transform.position = transform.position + new Vector3(0, 0, 0);
            Destroy(obs, 15);
            timer = 0;
        }    

        timer += Time.deltaTime;
    }
}

this is the code i used to spawn and destroy the object.

i tried running the code now and its now not only spawning at all -_-

using UnityEngine;

public class obstaclemovement : MonoBehaviour
{
    public float speed = 5f;


    void Update()
    {
        transform.position += Vector3.back * speed * Time.deltaTime;
    }
}

this is the code i used to set the speed to spawn the objects and determine its postion

also sorry for being late. I am having exams.

I’m not seeing anything wrong with either of your code snippets there. Are you sure all the correct values are set up in the inspector and that you don’t have any other errors?

the values are correct and there are no errors in console.

You Have any other code that can make it work ?As I am a beginner in c# as I work mostly with JS and python

@GroZZleR I tried running this code and now the obstacles are generating but as the same level as my ground plane.

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

public class objectSpawner : MonoBehaviour
{

    public GameObject player;
    public GameObject[] trianglePrefabs;
    private Vector3 SpawnObstaclePosition;


   
    void Update()
    {
        float distanceToHorizon = Vector3.Distance(player.gameObject.transform.position, SpawnObstaclePosition);
        if (distanceToHorizon < 120)
        {
            spawnTriangles();
        }
    }

    void spawnTriangles()
    {
        SpawnObstaclePosition = new Vector3(0, 0, SpawnObstaclePosition.z + 30);
        Instantiate(trianglePrefabs[(Random.Range(0, trianglePrefabs.Length))], SpawnObstaclePosition, Quaternion.identity);

    }
}

can you tell me help me to fix it. and also how to destroy them after some time?

You already have a way to set the position with your SpawnObstaclePosition, so change that if you want to set them at different heights.

To destroy them after a certain time, call Destroy(object, time) like you were doing in your first code snippet:

GameObject clone = Instantiate(trianglePrefabs[(Random.Range(0, trianglePrefabs.Length))], SpawnObstaclePosition, Quaternion.identity);
Destroy(clone, 15f);

so after writing this code its working but there are 2 problem. 1)what to write in place of ‘clone’
2)the objects created stick out of my ground plane like this->

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

public class objectSpawner : MonoBehaviour
{

    public GameObject player;
    public GameObject[] trianglePrefabs;
    private Vector3 SpawnObstaclePosition;
    public Object TrianglePrefabs;

    public

    void Update()
    {
        float distanceToHorizon = Vector3.Distance(player.gameObject.transform.position, SpawnObstaclePosition);
        if (distanceToHorizon < 120)
        {
            spawnTriangles();
        }
    }

    void spawnTriangles()
    {
        SpawnObstaclePosition = new Vector3(0, 1, SpawnObstaclePosition.z + 30);
        Instantiate(trianglePrefabs[(Random.Range(0, trianglePrefabs.Length))], SpawnObstaclePosition , Quaternion.identity);
        Destroy(clone, 15f);

    }
}