Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem integrating a lobby into a network game

Discussion in 'Multiplayer' started by IlluminatorX, Jan 12, 2016.

  1. IlluminatorX

    IlluminatorX

    Joined:
    Nov 13, 2015
    Posts:
    1
    I have some issues to get a NetworkLobby attached to a working network game.

    I need this Lobby to let a joined player chose a "team" and by that which player prefab to get spawned.

    The Situation/ Idea:

    There are two main scripts that are running. A NetworkLobbyManager_custom that extends the NetworkLobbyManager and a LobbyPlayer_custom that extends the LobbyPlayer. Both are the standard assets for a Lobby. The NetworkLobbyManager is atached to a GameObject and the LobbyPlayer_custom is attached to a prefab that I use as lobby prefab.

    The NetworkLobbyManger_Custom manages the Canvases and Buttons within my menu and lobby scenes. It also etablishes a connection between server an clients.

    The LobbyPlayer_custom is there to save the team the player chose and to change to the game scene on the client.

    The App works like this:
    1. the NetworklobbyManager_custom lets you start a host/ join as client.
    2. after you estalished a connection a canvas with two buttons to choose the team pops up
    3. when a button is clicked the team is checked for free slots and the chosen team should be saved in the LobbyPlayer_custom script

    This is where I have a problem. I can't get acces to the LobbyPlayerPrefabs that contain the scripts or the scripts to save the choice.

    When I try to get access to the LobbyPlayer_custom script to find the id of the LobbyPlayer_custom that made the choice, I get a NullPointerException.

    Code (CSharp):
    1. private string GetLobbyPlayersIdByIp (string ip)
    2.     {
    3.       LobbyPlayer_custom lobbyPlayer;
    4.         for(int i=0; i< maxPlayers; i++)
    5.         {
    6.             if ((lobbySlots[i] != null))
    7.             {   // nullpointer here
    8.                 lobbyPlayer = lobbySlots[i] as LobbyPlayer_custom;
    9.                 if (lobbyPlayer.GetPlayerIP().Equals(ip))
    10.                 {
    11.                     return lobbyPlayer.GetPlayerUID();
    12.                 }
    13.             }
    14.         }
    15.         return "null";
    16.     }
    I also tried to get access to the LobbyPlayer_custom script through finding all LobbyPlayerPrefab instances by something like this:
    Code (CSharp):
    1. LobbyPlayer_custom lobbyPlayer;
    2.         Object[] objs = Resources.FindObjectsOfTypeAll(typeof(GameObject));
    3.  
    4.         foreach (Object obj in objs)
    5.         {
    6.             if (obj.name.Contains("player") || obj.name.Contains("Player"))
    7.             {
    8.                 lobbyPlayer = (obj as GameObject).GetComponent<LobbyPlayer_custom>();
    9.                 //none of the lobbyPlayers methods are accessible
    10.             }          
    11.         }

    Does anyone have any idea how access all the LobbyPlayer scripts of the spawned Prefabs?

    Thanks a lot.
     
  2. Sofia0011

    Sofia0011

    Joined:
    Nov 2, 2015
    Posts:
    1
    I kind of have the same problem. Does anyone have a clue? Or is there maybe somewhere a working example of a multiplayer lobby for different kinds of player prefabs to get spawned?
     
  3. _FLX

    _FLX

    Joined:
    Nov 10, 2015
    Posts:
    85
    https://github.com/santiihoyos/Netw...sts_Unity3D/Assets/Scripts/Lobby/LobbyHook.cs

    LobbyHook is the solution to set player properties using lobbyPlayer data.

    Code (csharp):
    1. public virtual void OnLobbyServerSceneLoadedForPlayer(NetworkManager manager, GameObject lobbyPlayer, GameObject gamePlayer) {
    2.     gamePlayer.name = lobbyPlayer.nickname;
    3.     gamePlayer.setTeam(lobbyPlayer.team_id);
    4.     gamePlayer.setCharacter(lobbyPlayer.character_id);
    5.     //etc...
    6.  }