random obstacle generator

in a 3 lane system i want the obstacles to generate upto a certain distance in front of the player,but the script i’ve made isn’t working.take a look

using UnityEngine;
using System.Collections;

public class Obstacle_generate : MonoBehaviour {

// Use this for initialization
public Transform obstacl;
public float y;
public float[] chooseX;
public int randomX;
public int pp;
void Start () {
	chooseX=new float[3];
	chooseX [0] = -1.5f;
	chooseX [1] = 0;
	chooseX [2] = 1.5f;


}

// Update is called once per frame
void Update () {
label1:pp = (int)loco.playerDistance;
	randomX = Random.Range(0,3);

	if(pp%10==0)
	{
		y = Random.Range(pp,pp + 30);
		Instantiate (obstacl, new Vector3 (chooseX[randomX], y,0),Quaternion.identity);
		goto label1;
	}

}

}

where loco.playerDistance is the y coordinate of the player object
it spawns too many obstacles sometimes even on top of each other
how can i make it spawn after a specific distance each and only once at that y coordinate

This is untested as I’m away from my normal computer atm, but it should give you an idea of how to do it I hope:

public float yDistance = 10;
public float minSpread = 5;
public float maxSpread = 10;

public Transform playerTransform;
public Transform obstaclePrefab;

float ySpread;
float lastYPos;

void Start(){
	lastYPos = Mathf.NegativeInfinity;
	ySpread = Random.Range(minSpread, maxSpread);
}

void Update () {
	if(playerTransform.position.y - lastYPos >= ySpread){
		float lanePos = Random.Range(0, 3);
		lanePos = (lanePos-1)*1.5f;
		Instantiate(obstaclePrefab, new Vector3(lanePos, playerTransform.position.y + yDistance, 0), Quaternion.identity);
		
		lastYPos = playerTransform.position.y;
		ySpread = Random.Range(minSpread, maxSpread);
	}
}

Just drag your player object and obstacle prefabs onto the appropriate fields in the inspector.

hey, i just read this, and ive copied and pasted it but i have a problem, it says : Assets/Scripts/Ramdom.cs(37,16): error CS0103: The name `loco’ does not exist in the current context
What do I have to do? Thx
Here is my script btw
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ramdom : MonoBehaviour
{

public float startDistance = 10;
public float yDistance = 100;
public float minSpread = 5;
public float maxSpread = 10;

public Transform playerTransform;
public Transform obstaclePrefab;

float ySpread;
float lastYPos;

void Start()
{
    ySpread = Random.Range(minSpread, maxSpread);
    lastYPos = playerTransform.position.y + (startDistance - ySpread - yDistance);
}

void Update()
{
    if (playerTransform.position.y - lastYPos >= ySpread)
    {
        float lanePos = Random.Range(0, 3);
        lanePos = (lanePos - 1) * 1.5f;
        Instantiate(obstaclePrefab, new Vector3(lanePos, lastYPos + ySpread + yDistance, 0), Quaternion.identity);

        lastYPos += ySpread;
        ySpread = Random.Range(minSpread, maxSpread);
    }

    while (loco.playerDistance > 9.5 % loco.playerDistance < 10.5)
    {
        Instantiate();
    }
    if (loco.playerDistance > transform.position.y)
    {
        Debug.Log("Player distance: " + loco.playerDistance);
        Debug.Log(gameObject + " is at height " + transform.position.y);
        Destroy(gameObject);
    }
}

}