# How do I get objects using layer name?

I am creating an FPS game in local multiplayer. One player creates the hot-spot game, and the other one connects through WiFi network and plays the game.

I want to find all of the player objects on the network that have a specific layer name, and store them into an array when the player spawns on the network. I've been able to add the object to the array, but I can't find the layer's objects with what I found with Google searches.

LayerMask.NameToLayer("LayerName");



This returns the name of layer and layer index. How can I get the objects using a specific layer name, and also store it in to an array?

• Is there any reason you want to use layer for this? It seems odd to use layer to find your players.. – Gnemlock Jan 25 '17 at 13:06
• instead of layer i do this using tag name i want to create some fix distance between all players in network so i want to get all the players objects and then put into the Raycast to any player into the ray then set the stoping distance between them. – Darshan Soni Jan 25 '17 at 13:52
• i get the object of player in network and add in ArrayList also but the problem is when player can spawn then previous player remove to the arraylist and add the last spawn player in arraylist .want to not add player in array but when player can spawn then array previous value as it is and add last spawn player in that array. – Darshan Soni Jan 25 '17 at 13:54
• As far as I can tell, that doesn't explain why you have to initially grab these player references with a layer.. – Gnemlock Jan 25 '17 at 14:33

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)
{
}
}
if (goList.Count == 0)
{
return null;
}
return goList.ToArray();
}


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 ) )

if( !gameObjectsByLayer[player.layer].Contains(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 ;
}

public List<GameObject> GetPlayersFromLayer( int layer )
{
if( !gameObjectsByLayer.ContainsKey( layer ) )
return null ;
else
return gameObjectsByLayer[layer] ;
}

}

• thanks for answer @hellium , but in my game when player spawn then add to array so this function call frequently but as u say not use frequently because it is quite slow and i use this every time when player spawn in network and add to array, like refresh the array. – Darshan Soni Jan 25 '17 at 12:15
• @darshan-soni : Then, it may be a better idea to update a dynamic List or a Dictionnary when a player connects. See my updated answer. – Hellium Jan 25 '17 at 14:14