# Calculating the Exact Center of Multiple Objects

So I’ve got this object which ‘animals’ are supposed to move around and stuff. The problem with this is that I can’t get this point to be in the exact center of the GameObjects. If you select multiple objects in the editor, the translation thingy is located in the center of these objects, and that is the point I want, but for some reason, I’m always a couple of units off…

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class groupingObjectController : MonoBehaviour {
List animalObjects;
List animalTrans = new List();
Vector3 controllerPos = new Vector3();
float count = 0;
Vector3 center = new Vector3(0,0,0);
Vector3 centroid;

``````void Start(){
StartCoroutine("controllerPosition");
}

IEnumerator controllerPosition(){
while (enabled){
animalObjects = new List<GameObject>(GameObject.FindGameObjectsWithTag("animal"));
foreach (GameObject animal in animalObjects){
center += animal.transform.position;
count++;
}
centroid = center/count;
//controllerPos = controllerPos/animalTrans.Count;
transform.position = centroid;
print ("Yielding");
yield return true;
}
}
``````

}

I think you use ‘Bounds’

So after some experimentation, I believe you are using the “weighted center” whereas unity uses the center of the combined bounding box. Imagine you have 20 objects placed on a line that is 100 units long, and 19 of them are at 0 and 1 is at 100. Your “center” would be close to 0 while Unities “center” would show at 10. If you want to match unity do something like

``````private List<Renderer> animalRenderers;
private List<GameObject> animalObjects;

void Start () {

animalObjects = new List<GameObject> (GameObject.FindGameObjectsWithTag ("animal"));
animalRenderers = new List<Renderer> ();
foreach (GameObject animal in animalObjects) {
}
StartCoroutine ("controllerPosition");
}

IEnumerator controllerPosition () {
while (enabled) {

Bounds totalBounds = new Bounds ();
if (animalRenderers.Length > 0) totalBounds = animalRenderers[0].bounds;

foreach (Renderer r in animalRenderers) {
totalBounds.Encapsulate (r.bounds);
}

centroid = totalBounds.center;
//controllerPos = controllerPos/animalTrans.Count;
transform.position = centroid;
print ("Yielding");
yield return true;
}
}
``````