# Unity bounds includes the center of the scene

I'm trying to get the bounds of an object and put a box around it.

Sounds simple right? I just encapsulate all the bounds together and use its center and size to do so.

But the problem is one corner of the box is always at the center of the scene, like a part of the object is there, unless the center of the scene is in the bounds.

This is when object is at the center

This is when it isn't

This is the code i'm using:

    public static Bounds Calculate(Transform TheObject)
{
Renderer renderer = TheObject.GetComponent<Renderer>();
Bounds combinedBounds = new Bounds();
if (renderer != null)
combinedBounds = renderer.bounds;
var renderers = TheObject.GetComponentsInChildren<Renderer>();
foreach (Renderer render in renderers)
{
if (render != renderer) combinedBounds.Encapsulate(render.bounds);
}
return combinedBounds;
}

• When you initialize combinedBounds = new Bounds(), does this default constructor give you a set of bounds around (0, 0, 0)? This might be the source of your origin point forcing its way into your bounds. Dec 20, 2016 at 15:16
• thank you @DMGregory that was the problem. should I delete this question now that I now how simple the mistake was? Dec 20, 2016 at 15:24
• Nah, someone else might have the same issue and go looking for ideas. Feel free to write up how you solved it in an answer, and include your modified code. Dec 20, 2016 at 15:26
• That said, you should actually post the solution as an answer, and accept it, so others know it actually was the solution. Dec 20, 2016 at 23:25

As @DMGregory said the problem was that if I create a new bound to initialize the variable, it means I'm creating a bound that is located in the center of the scene and has 0 extents. So all I had to do was to rearrange the order of my code :

    public static Bounds Calculate(Transform TheObject)
{
var renderers = TheObject.GetComponentsInChildren<Renderer>();
Bounds combinedBounds = renderers[0].bounds;
for(int i = 1; i < renderers.Length; i++)
{
combinedBounds.Encapsulate(renders[i].bounds);
}
return combinedBounds;
}

• Don't forget to pick this as your answer :) Dec 22, 2016 at 10:42