Calculating the Midpoint of up to 6 Objects

The game I’m working is a one player, 3D rendered, 2.5D platformer/puzzle game in which the player starts out with only one player object, but as they search through a level they can find boxes to break which spawns another player object that is controlled simultaneously with the others, similarly to the Double Cherry in Super Mario 3D World. Whenever a new player object is spawned it is given the lowest numbered available name with a maximum of 6, so, for example, if the currently existing players alive are player 1 and player 2, the next player will be named player 3. There are also slightly more complex possibilities, such as if player 1 died and is now set to null, and the only existing players are player 4 and player 2, the next player will be named player 1, and the one after that will be player 3, etc.

With only one player object the camera is very simple; it simply targets that player object. When there are additional player objects, however, I’d like to find the midpoint of them for the camera to target by adding the positions of the player objects and dividing them by the amount of players, which I have stored in an integer, playerCount. The problem is that, each player can spawn with the names Player 1 up through Player 6 (I’ve set the limit of player objects to 6) so in order to find the midpoint of any possible combination of players the only method I could think to use was 36 if/else if statements like these:

        if (player1 != null && player2 == null && player3 == null && player4 == null && player5 == null && player6 == null)//1
        {
            midpoint = player1.transform.position;
        }
        else if (player1 != null && player2 != null && player3 == null && player4 == null && player5 == null && player6 == null)//1 2
        {
           midpoint = (player1.transform.position + player2.transform.position) / playerCount;
        }
        else if (player1 != null && player2 != null && player3 != null && player4 == null && player5 == null && player6 == null)//1 2 3
        {
            midpoint = (player1.transform.position + player2.transform.position + player3.transform.position) / playerCount;
        }
//etc

This pattern of checking to see what players currently are not null and then calculating the midpoints of said players continues for over 100 lines and although it “works” it’s not very efficient.

Another method I tried that almost works was simply calculating the midpoint of all players and dividing it by the number of players right from the start like so:

midpoint = (player1.transform.position + player2.transform.position + player3.transform.position + player4.transform.position + player5.transform.position + player6.transform.position) / playerCount;

But this of course returns a NullReferenceError whenever there aren’t already all six player objects spawned, meaning the midpoint is only calculated when there are all 6 player objects.

So now that that huge description is out of the way, is there a better way to find the midpoint of the 6 player objects that are spawned than testing all 36 possible combinations of player object combinations through if statements? Or is there a way to fix my second method so that it doesn’t return a NullReferenceError? Feel free to ask for further elaboration or code snippets in case I wasn’t descriptive enough about what I’d like to do.

I did it like this: Have the player GameObjects in a list so there are no null reference errors. If there still are, then clean the list before looping through it.

List<GameObject> players;
 float followSpeed = 10f;

 void SetCameraPos()	{

		float min_x = 0;
		float min_y = 0;
		float min_z = 0;
		float max_x = 0;
		float max_y = 0;
		float max_z = 0;

		float[] playerPositions_x = new float[players.Count];
		float[] playerPositions_y = new float[players.Count];
		float[] playerPositions_z = new float[players.Count];
		int i = 0;
		foreach(GameObject playerObject in players) {
                    
			playerPositions_x *= playerObject.transform.position.x;*

playerPositions_y = playerObject.transform.position.y;
playerPositions_z = playerObject.transform.position.z;

* i++;*
* }*

* min_x = Mathf.Min(playerPositions_x);
max_x = Mathf.Max(playerPositions_x);
min_y = Mathf.Min(playerPositions_y);
max_y = Mathf.Max(playerPositions_y);
min_z = Mathf.Min(playerPositions_z);
max_z = Mathf.Max(playerPositions_z);*

* float newPos_x = min_x + (Mathf.Abs(max_x - min_x) / 2);
float newPos_y = min_y + (Mathf.Abs(max_y - min_y) / 2);
float newPos_z = min_z + (Mathf.Abs(max_z - min_z) / 2);*

* Vector3 newCameraPos = new Vector3(newPos_x, newPos_y, newPos_z);*

_ transform.root.transform.position = Vector3.Lerp(transform.root.transform.position, newCameraPos, Time.deltaTime * followSpeed);
* }*_