# How to measure the height of a stack of objects?

Hello. I’m creating a 2D block stacking game where you instantiate blocks from a x-axis translating spawner object. The problem I’m facing is when the stack gets too high and close to the spawner, I need to move the spawner and game camera up on the Y-axis. Is there a way to measure a stack of prefabs to get their height so I know when to move my camera and spawner? I’m working in C#. Thanks in advance.

I’ll assume you have a record, or array of all the dropped blocks: loop through all the dropped blocks (exclude the still “falling” blocks by checking their velocity), and check their position. Note the block with the largest position.y value, and add half of that blocks height to this value to get the height of the stack. (if blocks vary in size, do the half-block height addition FIRST, and note the highest value of that.) Note: this assumes the “bottom” of the stack is at y=0, if not, you will need to adjust the result to compensate.

Better yet, you can store the hieghtOfTheStack, and have each block (possibly) update this value “OnCollision”.

I think possibly using Bounds may be the best solution as I can check the height of the parent object that contains all the block children. I’ve added some code to the solution that is part of the way there but is not increasing the parentBounds.max.y value as I add to the stack. Any ideas? Code below.

``````if  (Input.GetMouseButtonDown(0)) {
Instantiate (fallingBrick, spawnPosObj.transform.position, Quaternion.identity, parentStack.transform);
var parentBounds = GetMaxBounds (parentStack);
Debug.Log (parentBounds.max.y);
}

}

Bounds GetMaxBounds(GameObject g) {
var b = new Bounds(g.transform.position, Vector3.zero);
foreach (Renderer r in g.GetComponentsInChildren<Renderer>()) {
b.Encapsulate(r.bounds);
}
return b;
}
``````