Procedural placement of objects on plane with obstacles

The Setup:
Everything is on the XZ plane for now - I’m not taking height into consideration.
Regarding GameObjects, I have a plane with obstacles on it (Primitive Cubes and spheres).

The Aim:
I am trying to make a “path” of objects (cubes) that start at a random point.
I need to place consecutive cubes either beside, or above or below the last placed cube.
The cubes that are placed should not collide with the obstacles (This works fine).

The Problem:
Right now, I generate one cube randomly and then generate cubes to the left of that, and then to the right. The Z value is randomized for now.

My code for now is:

    private GameObject cube; // Random cube
    private int numberCube = 0;
    private Vector3 oldPosition;   

    void Start()
    {
        oldPosition = new Vector3(Random.Range(-21.0f, 21.0f), 2.5f, Random.Range(-21.0f, 21.0f));
    }

    // Update is called once per frame
    void Update()
    {
        // Generate 10 cubes
        if(numberCube < 10)
        {
            generateCube();
            numberCube++;
        }
     }

void generateCube()
    {
        cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube.tag = "Path";
        cube.AddComponent<S_CheckGrid>();
        cube.AddComponent<Rigidbody>();
        cube.GetComponent<Rigidbody>().isKinematic = false;
        cube.transform.position = oldPosition;
        cube.transform.localScale = new Vector3(2, 2, 2);

        float randFloat = Random.Range(0.1f, 1.2f); // Random increment for the X-Coordinate

        // Change the position for the next cube
        if (cube.transform.position.x - randFloat > -21)
        {
            oldPosition = new Vector3(cube.transform.position.x - randFloat, 2.5f, Random.Range(-21.0f, 21.0f));
        }
        else if (cube.transform.position.x + randFloat < 21)
        {
            oldPosition = new Vector3(cube.transform.position.x + randFloat, 2.5f, Random.Range(-21.0f,21.0f));
        }

        Debug.Log("Cube generated");
    }

I don’t fully understand your problem. If you want to deconflict placement of objects, keep track of the ones you placed, then loop through them to make sure the next one isn’t “too close” to the previous ones. If it is, choose a new location.