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.

Unity Multiplayer [Resolved With Palliative]NetworkManager.OnServerAddPlayer is not called from ClientScene.AddPlayer

Discussion in 'Multiplayer' started by dennysmvn, Sep 25, 2015.

  1. dennysmvn


    Jul 9, 2015
    Hi guys,

    This issue is killing me. I`ll explain the full cenario:

    I`m using MatchMaker to create, list and join matches. Nothing`s wrong with that, and I also have my custom NetworkManager with autoCreatePlayer checked, my playerPrefab, online and offline scenes set.

    My online scene have scripts that needs player`s references. But the player not already exists in online scene, so what I did was:

    In my custom NetworkManager I overrided OnServerAddPlayer method, and there I resolved all dependencies that needed the player instance, before calling NetworkServer.AddPlayerForConnection, and that worked, but ONLY for host player.

    When another player connects as a client, the method OnClientConnect is called, but when I call ClientScene.AddPlayer(conn, 0) from there, no OnServerAddPlayer method is called for the client. It seens like AddPlayer Message is not being sent, and I don`t know why.

    Online documentation tells that every ClientScene.AddPlayer will call OnServerAddPlayer. I already tried not to overried OnClientConnect method, but nothings changeded.

    What can be wrong? There is my custom NetworkManager code:

    Code (csharp):
    2. public override void OnClientConnect(NetworkConnection conn)
    3.     {
    4.         Debug.Log("Entered OnClientConnect");
    5.         ClientScene.AddPlayer(conn, 0);
    6.     }
    8.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    9.     {
    10.         Debug.Log("Entered OnServerAddPlayer");
    11.         var player = (GameObject)GameObject.Instantiate(playerPrefab, GetStartPosition().position, Quaternion.identity);
    12.         if(NetworkManager.singleton.numPlayers > 0){
    13.             player.tag = "Default";
    14.         }else{
    15.             player.tag = "Player";
    16.         }
    17.         if(NetworkServer.AddPlayerForConnection(conn, player, playerControllerId)){
    18.             player.GetComponent<PlayerNetworkSetup>().SetUpPlayerDependencies(player.GetComponent<Player>());
    19.         }
    21.     }
    Thanks for helping, and sorry for my bad English.
  2. dennysmvn


    Jul 9, 2015
    Hi guys,

    I resolved this problem with a different sollution.

    Instead of overriding OnServerAddPlayer method, I've created a new script that extends from NetwrokBehavior and attached it to the player.

    In that script I overrided OnStartLocalPlayer.

    So every new local player created will call this method and there I resolved all player's prefab dependencies ;).
    FullMe7alJacke7 likes this.