Random Spawn in X axis and Z

Hi guys,

am currently trying to figure out how I can add another axis into this script but i cant figure it out, am trying to create a endless runner and am able to generate the prefabs however I want to be able to spawn them with in a limited X-axis, for example spaws the tile between -2 and 2 while still spawning in the z axis, anyhelp would be greate and trying to learn as much as I can.

here is the code

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

public class Obstacles : MonoBehaviour
{
    public GameObject[] obsPrefab;
    private Transform playerTransform;
    public float spawnZ = 0;
    public float spawnX = 0;
    public int obsOnSceen = 15;
    public float ObsLength = 0;

    public float osbDestroy = 10f;

    private List<GameObject> activeObs;

    private int lastPrefabIndex = 0;


    // Use this for initialization
    void Start ()
    {
        activeObs = new List<GameObject>();

        obsPrefab = Resources.LoadAll<GameObject>("Group");

        playerTransform = GameObject.FindWithTag("Player").transform;

        for(int i = 0; i < obsOnSceen; i++)
        {
            if(i < 2)
            {
                GenerateOBS(0);
            }
            else
            {
                GenerateOBS();
            }
        }
    }
   
    // Update is called once per frame
    void Update ()
    {
        if (playerTransform.position.z - osbDestroy > (spawnZ - obsOnSceen * ObsLength))
        {
            GenerateOBS();
            DeleteOBS();
        }
    }

    private void GenerateOBS(int ObsIndex = -1)
    {
        GameObject go;

        if(ObsIndex == -1)
        {
            go = Instantiate(obsPrefab[OBSRandom()]) as GameObject;
        }

        else
        {
            go = Instantiate(obsPrefab[ObsIndex]) as GameObject;
        }

        go.transform.SetParent(transform);
        go.transform.position = Vector3.forward * spawnZ;
        spawnZ += ObsLength;

        activeObs.Add(go);
    }

    private void DeleteOBS()
    {
        Destroy(activeObs[0]);
        activeObs.RemoveAt(0);
    }

    private int OBSRandom()
    {
        int randomIndex = lastPrefabIndex;

        randomIndex = Random.Range(0, obsPrefab.Length);

        lastPrefabIndex = randomIndex;

        return randomIndex;
    }
}

Try this (between line 68 and 69, for example):

go.transform.position += Vector3.right * Random.Range(-2, 2);

thank you for reply, unfortunaly its not working, its just spawning them in one location, its skipping the spawn in the z axis aswell :frowning: