Help with spawning random size sprites one after the other

Hello, I’m trying to spawn objects one after the other with random height.

The problem is that for that I need to get half the height of the previous object and half the height of the new object and calculate it as the new position.y of the new object.

I tried a lot of ideas, and search the forums & the web for a solution. i’m about to lose it :slight_smile:

This is my current code, anyone have an idea?

public GameObject cube;
Vector3 cubeHeight = new Vector3(0,0,0);

void Start()
    InvokeRepeating("SpawnTheCube", 0.5f, 0.5f);

void SpawnTheCube()

    GameObject newCube = (GameObject)Instantiate(cube, new Vector3(0, cubeHeight.y, 0), Quaternion.identity, transform);
    newCube.transform.localScale = new Vector3(newCube.transform.localScale.x, UnityEngine.Random.Range(1, 6), newCube.transform.localScale.z); //Randomize the height
    float newCubeHeight = newCube.transform.localScale.y / 2; //Half the height of the new spawned cube
    cubeHeight.y += newCubeHeight;


Bumping I would love to get any ideas here.

Just to clarify the idea is to spawn planes/cubes (in orthographic view) one after the other and give them random height.