Unity Mirror - NetworkServer Send Message To Target Client

4.6k views Asked by At

I'm not sure what I'm doing wrong here but I can't seem to get my message from the server to the client. Here is what I have so far:

protected virtual void RegisterHandlers(bool enable)
{
    if (enable)
    {
        NetworkServer.RegisterHandler<ClientRequestLoadScene>(OnClientRequestedToLoadScene);

        NetworkClient.RegisterHandler<ServerRequestLoadScene>(OnServerRequestLoadScene);
     }
     else
     {
         NetworkServer.UnregisterHandler<ClientRequestLoadScene>();

         NetworkClient.UnregisterHandler<ServerRequestLoadScene>();
     }
}

The above is called when the instance starts to register a new handler. Then I have the client call:

ClientRequestLoadScene msg = new ClientRequestLoadScene();
msg.scene = scene;
NetworkClient.Send(msg);

This is received by the server fine. Then the server runs the following:

private void OnClientRequestedToLoadScene(NetworkConnection conn, ClientRequestLoadScene msg)
{
  ...
  ...
   ServerRequestLoadScene server_msg = new ServerRequestLoadScene();
   server_msg.scene = msg.scene;
   NetworkServer.SendToClientOfPlayer(conn.identity, msg);
  ...
  ...
}

The above message is never received by the client. I have also tried: NetworkServer.SendToAll(msg); and that is never received by the client either. What am I doing wrong?

1

There are 1 answers

0
wesleywh On BEST ANSWER

The issue with the above is with these lines:

server_msg.scene = msg.scene;
NetworkServer.SendToClientOfPlayer(conn.identity, msg);

It needed to be:

server_msg.scene = msg.scene;
conn.Send(server_msg);