How do you get the total scale size of a selected object?

Hello,
I am trying to figure out a way to write a javascript function where even if I were to provide my function with an argument contains an empty game object where the empty game object has children that have colliders attached to them, the function still returns the total x, y and z scale of the whole object provided. Is there already a Unity function that already does that? If so, this would be useful for many game developers.

Let’s say I have a sphere floating above a large cube (where the cube is my terrain), and I want to calculate where the very bottom of the sphere should be at ground level instead of the middle of the sphere being at ground level. I would first use the RayCast function to calculate the y position of where the middle of the sphere should be at ground level, but my goal is to calculate where the very bottom of the sphere should be instead of the middle. I would first need to find out the total y scale of my sphere, divide it by 2 and increase the sphere’s y axis by half its y scale after using the Raycast function in order to make it look like the very bottom of the sphere is touching the ground instead of the middle, but what if the sphere is a child of an empty game object where the empty parent has its own scale? I would get a different result.

I was wondering if there is a way where IF I were to select the object that I provide to the function that I am trying to write (in one of my scene windows), the function calculates and returns the total x, y and z scale of all the collider highlights in 3D world space that would become visible in my scene windows IF I were to select that object in one of my scenes. I am not trying to get a world-space based size, so no matter what the rotation of my object is, I am just interested in getting the same scale results (like a bounding box).

Thank you,
Michael S. Lowe

I figured it out…

function getTotalBounds(o) : Vector3 {
var boundx = 0.0;
var boundy = 0.0;
var boundz = 0.0;
if (o.collider) {
boundx = o.collider.bounds.size.x;
boundy = o.collider.bounds.size.y;
boundz = o.collider.bounds.size.z;
} else {
boundx = 0.0;
boundy = 0.0;
boundz = 0.0;
}
var bound : Vector3 = Vector3(boundx,boundy,boundz);
for (var child : Transform in o.transform) {
bound = getTotalBounds(child);
if (bound.x > boundx) {
boundx = bound.x;
}
if (bound.y > boundy) {
boundy = bound.y;
}
if (bound.z > boundz) {
boundz = bound.z;
}
}
return Vector3(boundx, boundy, boundz);
}

Actually I answered that recently here:

function GetBounds (thisGameObject : GameObject) : Bounds {
	var totalBounds = thisGameObject.GetComponentInChildren(Collider).bounds;
	var colliders = thisGameObject.GetComponentsInChildren.<Collider>();
	for (col in colliders) totalBounds.Encapsulate(col.bounds);
	return totalBounds;
}

Please don’t do this: “function getTotalBounds(o) : Vector3 {”. You haven’t defined the type of “o”, which makes it very slow (relatively speaking) and problematic. Also, “o” is really not descriptive at all as a variable name…when reading code, it doesn’t give any hint as to what it means.

–Eric

var colliders = thisGameObject.GetComponentsInChildren.();

Unity doesn’t recognize this line.