Auto-calculate bounds - Created cube at wrong position


So here’s what I am doing. I have a quite complex game object consisting of various child objects. I automatically calculate the correct bounds size using this code (also considering rotation):

	private void CalculateBounds()
		Quaternion currentRotation = this.transform.rotation;
		this.transform.rotation = Quaternion.Euler(0f,0f,0f);

		Bounds bounds = new Bounds(this.transform.position,;

		foreach(Renderer renderer in GetComponentsInChildren<Renderer>())

		Vector3 localCenter = - this.transform.position;

		// Making a cube
		GameObject cube = GameObject.CreatePrimitive (PrimitiveType.Cube);

		cube.transform.position =;
		cube.transform.localScale = bounds.extents * 2;

		this.transform.rotation = currentRotation;

		cube.transform.localRotation = this.transform.rotation;

The cube has the correct size, but is has a certain offset (see screenshot). I just can’t figure out why. Any ideas?

Ok, it seems the pivot point of the blue object differs from the cube’s pivot. And that’s why the final position of the objects differ after the rotation. Is there any way to solve this problem?