It seems there is not any built-in function to do what you want, but you can write your own function to find every object in a given layer. However, be careful, the function is quite slow, so do not use it frequently :
GameObject[] FindGameObjectsInLayer(int layer)
{
var goArray = FindObjectsOfType(typeof(GameObject)) as GameObject[];
var goList = new System.Collections.Generic.List<GameObject>();
for (int i = 0; i < goArray.Length; i++)
{
if (goArray[i].layer == layer)
{
goList.Add(goArray[i]);
}
}
if (goList.Count == 0)
{
return null;
}
return goList.ToArray();
}
Source : http://answers.unity3d.com/questions/179310/how-to-find-all-objects-in-specific-layer.html
If you have to frequently update the list, I advise you to update a Dictionary when a player connects / disconnect / changes layer. The following class can't be attached to a gameobject, simply instantiate it inside your players manager. I haven't tested the following code :
using UnityEngine ;
using System.Collections.Generic ;
private class PlayersManagerByLayer
{
private Dictionary<int,List<GameObject>> gameObjectsByLayer ;
public PlayersManagerByLayer()
{
gameObjectsByLayer = new Dictionary<int,List<GameObject>>();
}
public void AddPlayer( GameObject player )
{
if( !gameObjectsByLayer.ContainsKey( player.layer ) )
gameObjectsByLayer.Add( player.layer, new List<GameObject>() );
if( !gameObjectsByLayer[player.layer].Contains(player ) )
gameObjectsByLayer[player.layer].Add( player ) ;
}
public void RemovePlayer( GameObject player )
{
if( gameObjectsByLayer.ContainsKey( player.layer ) && gameObjectsByLayer[player.layer].Contains(player ) )
gameObjectsByLayer[player.layer].Remove( player ) ;
}
public void MovePlayerToLayer( GameObject player, int newLayer )
{
RemovePlayer( player ) ;
player.layer = newLayer ;
AddPlayer( player ) ;
}
public List<GameObject> GetPlayersFromLayer( int layer )
{
if( !gameObjectsByLayer.ContainsKey( layer ) )
return null ;
else
return gameObjectsByLayer[layer] ;
}
}