Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

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

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

  1. dennysmvn

    dennysmvn

    Joined:
    Jul 9, 2015
    Posts:
    2
    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):
    1.  
    2. public override void OnClientConnect(NetworkConnection conn)
    3.     {
    4.         Debug.Log("Entered OnClientConnect");
    5.         ClientScene.AddPlayer(conn, 0);
    6.     }
    7.  
    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.         }
    20.  
    21.     }
    22.  
    Thanks for helping, and sorry for my bad English.
     
  2. dennysmvn

    dennysmvn

    Joined:
    Jul 9, 2015
    Posts:
    2
    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.