Auto-moving selected objects to the surface of another object below.

Ok, so I am making an editor script that will move all of the selected objects onto the surface of the object directly below.
I came up with the following code:

@MenuItem("Scripts/Normalize")
static function Normalize() {

    for (var obj : GameObject in Selection.gameObjects)
	{
		var hit : RaycastHit;
		if(Physics.Raycast(obj.transform.position, -Vector3.up, hit))
		{	
			var coll : Collider = obj.GetComponent(Collider);
			var bottom = coll.bounds.max.y
			obj.transform.position.y = obj.transform.position.y - hit.distance + bottom;
		}
    }
}

If I remove the bottom variable from the vertical translation, it results in the selections center of gravity being placed at the surface. With the bottom variable being taken into account, however, it doesn’t even make it to the ground… it moves downward slightly, but not to where it should.

Am I doing something wrong or is there a better way to detect the bottom of an object?

Try this:

@MenuItem("Scripts/Normalize")
static function Normalize() {

    for (var obj : GameObject in Selection.gameObjects)
	{
		var hit : RaycastHit;
		if(Physics.Raycast(obj.transform.position, -Vector3.up, hit))
		{	
			var coll : Collider = obj.GetComponent(Collider);
			obj.transform.position.y = obj.transform.position.y - hit.distance + coll.bounds.extents.y;
		}
    }
}

Hey, thanks, but I figured it out without using the collider. I forgot that the Mesh was also a component, so I did this:

@MenuItem("Scripts/Normalize")
static function Normalize() {

    for (var obj : GameObject in Selection.gameObjects)
	{
		var hit : RaycastHit;
		if(Physics.Raycast(obj.transform.position, -Vector3.up, hit))
		{	
			var meshFilter : Mesh = obj.GetComponent(MeshFilter).mesh;
			var bottom = meshFilter.bounds.min.y;
			var offset : Offset = obj.GetComponent(Offset);
			obj.transform.position.y = obj.transform.position.y - hit.distance + bottom + offset.offset;
		}
    }
}

The offset variable is from a separate script attached to each of the objects.