3

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?

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
wesleywh
  • 1,053
  • 1
  • 13
  • 30
  • 1
    Is there a reason for the -1? This is a legitimate question. I would love to hear the reason why you think this doesn't belong here. – wesleywh Dec 19 '20 at 02:09

1 Answers1

2

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);
wesleywh
  • 1,053
  • 1
  • 13
  • 30