# Average position of a set of GameObjects

Hi I am trying to familiarize myself with Unity, I want to get the average position of a list of objects.

Currently I have two 2D objects and I am trying to find the average position, I am using DrawRay to view visually. I expect the two lines to meet between the two objects - but they dont…

Update Code:

``````		//list of positions to average
List<Vector3> positions = new List<Vector3>();

//get some objects
GameObject[] arr = GameObject.FindGameObjectsWithTag("test");

for (var i = 0; i < arr.Length; i++) {
``````
• `````` }*
``````
• `````` //get the average of the objects in positions*
``````
• `````` Vector3 avg = GetMeanVector(positions);*
``````
• `````` //draw line from center of current gameObject to average position.*
``````
• `````` Debug.DrawRay(transform.position, avg, Color.red);*
``````
• `````` Debug.Log ("AV" + avg + "RL" + transform.position);*
``````

The GetMeanVector function found here, which looks ok: Find the average of 10 Vectors - Unity Answers

• private Vector3 GetMeanVector(List positions){*

• `````` if (positions.Count == 0)*
``````
• `````` 	return Vector3.zero;*
``````
• `````` float x = 0f;*
``````
• `````` float y = 0f;*
``````
• `````` float z = 0f;*
``````
• `````` foreach (Vector3 pos in positions)*
``````
• `````` {*
``````
• `````` 	x += pos.x;*
``````
• `````` 	y += pos.y;*
``````
• `````` 	z += pos.z;*
``````
• `````` }*
``````
• `````` return new Vector3(x / positions.Count, y / positions.Count, z / positions.Count);*
``````
• }*

I have some screenshots of the result.
This screenshot shows the rays in the scene view.
[70141-bird.jpg|70141]*
This screenshot prints the positions.
[70142-debug.jpg|70142]*
*
*
Any help appreciated thanks.

Try something like this…

``````for (var i = 0; i < arr.Length; i++) {

//draw line from center of current gameObject to average position.
Debug.DrawRay(arr*.transform.position, avg, Color.red);*
``````
• ``````  }*
``````

I had another look after I got home from work and it was DrawLine() that I wanted because I only wanted to check two points; however, for the sake of testing DrawRay() I needed to set the angle. So the code would change to:

``````Debug.DrawRay(transform.position, avg - transform.position, Color.green);
``````