I found out that the Boxcollider I get from the solution above was only expanding the bounds of all the children while leaving out the gameObject itself aswell as all other descendants, which confused me at first.
here is the code that will surround the gameobject and every descendant:
public void addBoundsToAllChildren()
{
if (boxCol == null)
{
boxCol = gameObject.GetComponent(typeof(BoxCollider)) as BoxCollider;
if (boxCol == null)
{
boxCol = gameObject.AddComponent<BoxCollider>();
}
}
Bounds bounds = new Bounds(Vector3.zero, Vector3.zero);
Renderer thisRenderer = transform.GetComponent<Renderer>();
bounds.Encapsulate(thisRenderer.bounds);
boxCol.center = bounds.center - transform.position;
boxCol.size = bounds.size;
allDescendants = gameObject.GetComponentsInChildren<Transform>();
foreach (Transform desc in allDescendants)
{
Renderer childRenderer = desc.GetComponent<Renderer>();
if (childRenderer != null)
{
bounds.Encapsulate(childRenderer.bounds);
}
boxCol.center = bounds.center - transform.position;
boxCol.size = bounds.size;
}
}