Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity Mirror - NetworkServer Send Message To Target Client

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?

like image 330
wesleywh Avatar asked Oct 14 '25 04:10

wesleywh


1 Answers

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);
like image 72
wesleywh Avatar answered Oct 18 '25 19:10

wesleywh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!