1
\$\begingroup\$

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

LayerMask.LayerToName(8);

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?

\$\endgroup\$
  • \$\begingroup\$ Is there any reason you want to use layer for this? It seems odd to use layer to find your players.. \$\endgroup\$ – Gnemlock Jan 25 '17 at 13:06
  • \$\begingroup\$ 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. \$\endgroup\$ – Darshan Soni Jan 25 '17 at 13:52
  • \$\begingroup\$ 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. \$\endgroup\$ – Darshan Soni Jan 25 '17 at 13:54
  • \$\begingroup\$ As far as I can tell, that doesn't explain why you have to initially grab these player references with a layer.. \$\endgroup\$ – Gnemlock Jan 25 '17 at 14:33
4
\$\begingroup\$

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] ;
    }

}
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Darshan Soni Jan 25 '17 at 12:15
  • \$\begingroup\$ @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. \$\endgroup\$ – Hellium Jan 25 '17 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.