Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. 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 'Connected Games' started by mcbauer, May 5, 2018.

  1. mcbauer

    mcbauer

    Joined:
    Oct 10, 2015
    Posts:
    352
    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:
    352
    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.