problems with generation of obstacles help

Hello everyone, I have two questions to which I can’t find a solution. When my obstacles are generated, it usually happens that sometimes they are generated one on top of the other, how do I make this not happen? The next question is that one of these obstacles is floating but as the map continues to be generated, the obstacle never appears floating again, it appears on the ground, how do I solve this?

This is my code

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

public class GameManager : MonoBehaviour
{
public GameObject menuPrincipal;
public GameObject menuGameOver;

public float velocidad = 2;
public GameObject col;
public GameObject piedra1;
public GameObject aleteo;
public GameObject aleteo2;
public Renderer fondo;
public bool gameOver = false;
public bool start = false;

public List<GameObject> cols;
public List<GameObject> obstaculos;
// Start is called before the first frame update
void Start()
{
    // Crear Mapa
    for (int i = 0; i < 21; i++)
    {
        cols.Add(Instantiate(col, new Vector2(-8 + i, -5), Quaternion.identity));
    }

    // Crear Piedras
    obstaculos.Add(Instantiate(piedra1, new Vector2(-3, -3), Quaternion.identity));
    obstaculos.Add(Instantiate(aleteo, new Vector2(5, -3), Quaternion.identity));
    obstaculos.Add(Instantiate(aleteo2, new Vector2(14, -1.6f), Quaternion.identity));
}

// Update is called once per frame
void Update()
{
    if (start == false) 
    {
    if (Input.GetKeyDown(KeyCode.X)) 
        {
            start = true;
        }
    }

    if (start == true && gameOver == true)
    {
        menuGameOver.SetActive(true);
        if (Input.GetKeyDown(KeyCode.X))
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        }
    }

    if (start == true && gameOver == false) 
    {
        menuPrincipal.SetActive(false);
        fondo.material.mainTextureOffset = fondo.material.mainTextureOffset + new Vector2(0.1f, 0) * Time.deltaTime;

        // Mover Mapa
        for (int i = 0; i < cols.Count; i++)
        {
            if (cols*.transform.position.x <= -10)*

{
cols*.transform.position = new Vector3(10, -5, 0);*
}
cols.transform.position = cols_.transform.position + new Vector3(-1, 0, 0) * Time.deltaTime * velocidad;
}
// Mover Obstaculos
for (int i = 0; i < obstaculos.Count; i++)
{
if (obstaculos*.transform.position.x <= -10)*
{
float randomObs = Random.Range(11, 18);
obstaculos*.transform.position = new Vector3(randomObs, -3, 0);*
}
obstaculos.transform.position = obstaculos.transform.position + new Vector3(-1, 0, 0) * Time.deltaTime * velocidad;
}
}
}
}_

Ok, here is a detailed answer of what you can do better :


1.Don’t move the ‘piedra’ and the ‘aleteo’, just move the Camera like :
(a) Create a new script called “CameraMove” and add it to the camera
(b) Add the following to that script

private float Speed; //Add this to the top of the script

void Start()
{
     Speed = 5f;
}

void Update()
{
     transform.position = new Vector2(transform.position.x * Speed * Time.deltaTime, transform.position.y)
}

2.Then make the following changes to your script :

 public GameObject menuPrincipal;
 public GameObject menuGameOver;
 public float velocidad = 2;
 public GameObject piedra1;
 public GameObject aleteo;
 public GameObject aleteo2;
 public bool gameOver = false;
 public bool start = false;
 public List<GameObject> obstaculos;
 public GameObject Camera; //Asign this in the inspector
 void Start()
 {
     // Crear Piedras
     obstaculos.Add(Instantiate(piedra1, new Vector2(-3, -3), Quaternion.identity));
     obstaculos.Add(Instantiate(aleteo, new Vector2(5, -3f), Quaternion.identity));
     obstaculos.Add(Instantiate(aleteo2, new Vector2(14, -1.6f), Quaternion.identity));
 }
 // Update is called once per frame
 void Update()
 {
     if (start == false) 
     {
     if (Input.GetKeyDown(KeyCode.X)) 
         {
             start = true;
         }
     }
     if (start == true && gameOver == true)
     {
         menuGameOver.SetActive(true);
         if (Input.GetKeyDown(KeyCode.X))
         {
              SceneManager.LoadScene(SceneManager.GetActiveScene().name);
         }
     }
     if (start == true && gameOver == false) 
     {
         menuPrincipal.SetActive(false);

     for (int i = 0; i < obstaculos.Count; i++)
     {
         if (Vector3.Distance(obstaculos*.transform.position, Camera.transform.position) >= 10)*

{
float randomObs = Random.Range(11, 18);
obstaculos_.transform.position = new Vector2(Camera.transform.position.x + randomObs, obstaculos*.transform.position.y);
}
}
}
}
----------
3.Drag the ‘Background’ image straight from the ‘Sprites’ folder to the ‘Scene View’
- After that a new GameObject will get created with the name of “Background” (This GameObject should only have a ‘SpriteRenderer’)
- Then adjust it’s ‘Scale’ to the appropriate size (For example : x = 15, y = 12) and make it a child of the ‘Camera’
- That should make the ‘Background’ follow along with the ‘Camera’ and ‘Player’
----------
4.Make the ‘Floor’ and ‘Player’ also a child of the ‘Camera’
----------*_

Hope this works :slight_smile:

No problem I really appreciate your help it’s okay

look what happens now