- Home /
 
 
               Question by 
               Wiktor9696 · May 24, 2019 at 05:49 PM · 
                randomrandom.rangegenerating  
              
 
              Semi-random prefabs generating
Hi, I created a script that generates one to four elements side by side. However, the problem is that some elements are generated in the same place as others in the x-axis, so they are superimposed. How to fix it?
 public GameObject[] Cars;
 private List<GameObject> CurrentCars = new List<GameObject>();
 private int CarIndex = 0;
 private int quantity;
 private void Start()
 {
     quantity = Random.Range(1, 4);
     GenerateCar();
     Debug.Log(quantity);
 }
 public void GenerateCar()
 {
     var Positions = new float[4];
     Positions[0] = -4.25f;
     Positions[1] = -1.4f;
     Positions[2] = 1.4f;
     Positions[3] = 4.25f;
     for (int i = 0; i < quantity; i++)
     {
         var index = Random.Range(0, Cars.Length);
         var index2 = Random.Range(0, Positions.Length);
         var prefab = Cars[index];
         var car = Instantiate(prefab);
         var car_pos = Positions[index2];
             CurrentCars.Add(car);
             car.transform.position = new Vector3(car_pos, 2.4f, 34.5f);
     }
 }
 
              
               Comment
              
 
               
              Your answer