Search Unity

  1. Full schedule for #UniteBerlin is now available! Featuring talks on our roadmap, hands-on labs and much more! Check it out!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

LOBBYMANAGER: Setting the spawn prefab index, prior to OnLobbyServerCreateGamePlayer

Discussion in 'Multiplayer Networking' started by mcbauer, May 5, 2018.

  1. mcbauer

    mcbauer

    Joined:
    Oct 10, 2015
    Posts:
    283
    I have a found a couple of topics on this, but no one is addressing my particular issue.

    Essentially, I have the system working pretty well, I have updated the Network Lobby from the asset store, to include my buttons for selecting the player character. I set the prefab index from those buttons in this script which is applied to the LobbyManager object:

    Code (CSharp):
    1. public class NetworkLobbyHook : LobbyHook
    2. {
    3.     public short p_index;
    4.  
    5.     public override void OnLobbyServerSceneLoadedForPlayer(NetworkManager manager, GameObject lobbyPlayer, GameObject gamePlayer)
    6.     {
    7.         LobbyManager nlm = NetworkLobbyManager.singleton.GetComponent<LobbyManager> ();
    8.         Debug.Log ("Before LobbyManager playerPrefabIndex = " + nlm.playerPrefabIndex);
    9.         nlm.playerPrefabIndex = p_index;
    10.         Debug.Log ("After LobbyManager playerPrefabIndex = " + nlm.playerPrefabIndex);
    11.  
    12.     }
    13.  
    14.     public void chooseRedBoy ()    {p_index = 2;}
    15.     public void chooseRedGirl (){p_index = 3;}
    16.     public void chooseBlueBoy (){p_index = 4;}
    17.     public void chooseBlueGirl (){p_index = 5;}
    18.     public void chooseYellowBoy (){    p_index = 6;}
    19.     public void chooseYellowGirl (){p_index = 7;}
    20.  
    21. }

    and in LobbyManager:

    Code (CSharp):
    1.  
    2. public short playerPrefabIndex;
    3. public override GameObject OnLobbyServerCreateGamePlayer(NetworkConnection conn, short playerControllerId)
    4.         {
    5.             Debug.Log ("CreateGamePLayer = " + playerPrefabIndex);
    6.             GameObject _temp = (GameObject)GameObject.Instantiate(spawnPrefabs[playerPrefabIndex], startPositions[conn.connectionId].position, Quaternion.identity);
    7.             NetworkServer.AddPlayerForConnection(conn, _temp, playerControllerId);
    8.             return _temp;
    9.         }
    My issue is that the setting of my prefab index happens after the lobby has created the game player. My output log looks like this:

    CreateGamePLayer = 2 (default value)
    Before LobbyManager playerPrefabIndex = 2
    After LobbyManager playerPrefabIndex = 7 ( value set from the button)

    How can I set the index prior to the server creating the game player?
     
  2. mcbauer

    mcbauer

    Joined:
    Oct 10, 2015
    Posts:
    283
    Is there a suggested approach to do this? I'm fine if how I'm going about this is wrong, I'm really in limbo until I can get past this.