[UNET] Clients position not being synced after being replaced with ReplacePlayerForConnection

Hello Community!

I’ve recently started on a simple multiplayer project to learn the ropes of UNET. I got a general idea of how everything works now, but I’ve stumpled upon a problem when trying to use NetworkServer.ReplacePlayerForConnection.

Thus far, I’ve got a NetworkManager that spawns a PlayerController (A lobby’ish kinda thing) that controls which team the player should join and setting the player name:

        public override void OnStartLocalPlayer()
        {
            if (!isServer) NetworkServer.Listen(NetworkServer.listenPort);
 
            base.OnStartLocalPlayer();
 
            gameManager = GameObject.Find("_GameManager").GetComponent<GameManager>();
 
            SetUI("Prematch");
 
            CmdSetPlayerName(gameObject, "Player_" + netId.Value.ToString());
        }

After joining the server, the playerName is being set through the server and then synchronized via SyncVar.

Now, the player has but choice and that is to join the game through a button. On click, this runs a method that sets the team and tells the server to create the player object with the NetworkServer.ReplacePlayerForConnection:

    void Btn_JoinGame()
        {
            if (gameManager.team1Players.Count <= 0)
            {
                CmdSetTeam(playerName + "_Controller", Team.One);
            }
            else
            {
                if (gameManager.team1Players.Count <= gameManager.team2Players.Count)
                {
                    CmdSetTeam(playerName + "_Controller", Team.One);
                }
                else
                {
                    CmdSetTeam(playerName + "_Controller", Team.Two);
                }
            }
 
            CmdAddPlayer(playerName + "_Controller");
 
            SetUI("Game");
        }

Now, the CmdAddPlayer does the following:

        [Command]
        void CmdAddPlayer(string target)
        {
            PlayerController pCtrl = GameObject.Find(target).GetComponent<PlayerController>();
 
            GameObject p = Instantiate(playerPrefab);
            p.GetComponent<Player>().PlayerName = pCtrl.playerName;
            p.GetComponent<Player>().Team = pCtrl.team;
 
            NetworkServer.ReplacePlayerForConnection(pCtrl.connectionToClient, p, 0);
        }

It all works quite well actually, the server instantiates the player on both the client and the server, and both players can move around and send messages to each other, but the clients position on the server is now not getting synchronized. I’m using the Transform Sync thingy that comes with UNET.

Any help is greatly appreciated!

EDIT:

So, I got this far → it works RARELY (1/200 builds). So I’m thinking that there’s some value that is being synchronized too late or too soon.

Bumping this thread, as I haven’t figured it out yet :slight_smile:

So, thus far I have figured out that the client does not send his position (despite being ready, got authority, is a client, got local player authority), which is why the server cannot see the clients move. But I have yet to figure out why the position is not being sent. I’m really reaching here guys :slight_smile:

Finally, I found an answer. By placing a ClientScene.SetLocalPlayer I got it to work.

This was my final code:

[Command]
  void CmdAddPlayer(string target)
  {
     PlayerController pCtrl = GameObject.Find(target).GetComponent<PlayerController>();
     NetworkIdentity pNID = pCtrl.GetComponent<NetworkIdentity>();
     NetworkConnection conn = pNID.connectionToClient;
     GameObject p = Instantiate(playerPrefab);
     p.GetComponent<Player>().Team = team;
     p.GetComponent<Player>().PlayerName = playerName;
     ClientScene.SetLocalObject(pNID.netId, p);
     NetworkServer.ReplacePlayerForConnection(conn, p, 0);
  }

I really hope someone this will help someone! :slight_smile:

Hey Dan. I’m new to Unet and instead of spawning a Player Object via OnServerAddPlayer(), I’m opting for an Empty with a Script that responds to GUI.

public class EmptyPlayer : NetworkBehaviour {

    public GameObject[] playerObj;

    [Command]
    public void CmdChangePlayer(int playerNum)
    {
        GameObject oldPlayer = this.gameObject;
        NetworkIdentity oldPlayerID = oldPlayer.GetComponent<NetworkIdentity>();
        NetworkConnection conn = oldPlayerID.connectionToClient;
        GameObject newPlayer = (GameObject)Instantiate(playerObj[playerNum]);
        ClientScene.SetLocalObject(oldPlayerID.netId, newPlayer);
        NetworkServer.ReplacePlayerForConnection(conn, newPlayer, 0);
    }
}

I’ve tried a couple things, but I’m grasping at straws. I saw your code in this post, but I was hoping you could condense it for just what I need and possibly explain to me WHY it works. The ‘player’ is spawning, but under another networkId. And I’m getting a console error “Object reference not set to an instance of an object”. (I’m not using a playerController). Also, the gameObject is registered within the Spawnable Reference list in the Network Manager.

EDIT: I’ve narrowed down the issue to being connectionToClient;
Debug.Log ("conn " + connectionToClient.ToString());‘oldPlayerID.netId’ checks out to be 0 which is true, but I’m not allowed to use that in ‘ReplacePlayerForConnection’. What gives?

EDIT 2: And you’re not allowed to cast netId to a NetworkConnection;

NetworkConnection conn = (NetworkConnection)oldPlayerID.netId;

Nice Dan! I intend on doing exactly what you did here (creating a more counterstrike style “lobby” GUI in the actual game scene vs. having a separate lobby scene, and replacing the lobby prefab with the final player prefab once they’re ready).

I have to admit I’m confused by your solution though… isn’t ClientScene meant to be used on the client himself? Any code in a command is going to be executed on the server, no?