Skip to main content
added 25 characters in body
Source Link
Hellium
  • 2.9k
  • 1
  • 13
  • 27

It seems there is not any built-in function to do what you want, but you can write your own function to do what you wantfind every object in a given layer. However, be careful, the function is quite slow, so do not use it frequently :

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

}

It seems there is not any built-in function to do what you want, but you can write your own function to do what you want. However, be careful, the function is quite slow, so do not use it frequently :

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

}

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 :

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

}
Adding generic types that seem to have gone missing in formatting.
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401
GameObject[] FindGameObjectsInLayer(int layer)
{
    GameObject[]var goArray = FindObjectsOfType(typeof(GameObject)) as GameObject[];
    System.Collections.Generic.Listvar goList = new System.Collections.Generic.ListList<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();
}
GameObject[] FindGameObjectsInLayer(int layer)
{
    GameObject[] goArray = FindObjectsOfType(typeof(GameObject)) as GameObject[];
    System.Collections.Generic.List goList = new System.Collections.Generic.List();
    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();
}
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();
}
added 214 characters in body
Source Link
Hellium
  • 2.9k
  • 1
  • 13
  • 27
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] ;
    }

}
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 List<GameObject> GetPlayersFromLayer( int layer )
    {
        if( !gameObjectsByLayer.ContainsKey( layer ) )
            return null ;
        else
            return gameObjectsByLayer[layer] ;
    }

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

}
added 1464 characters in body
Source Link
Hellium
  • 2.9k
  • 1
  • 13
  • 27
Loading
Source Link
Hellium
  • 2.9k
  • 1
  • 13
  • 27
Loading