Missing something important with ClientRpc (Unity Mirror)

1.3k views Asked by At

I'm having an issue with ClientRpc never being called any client objects. I've trolled the internet for hours, but I can't find any clues as to why my implementation isn't working. I'm following an order of events like so:

  1. Add players in a lobby
  2. Switch scene to the gameplay scene
  3. Generate random tiles on the server
  4. Send tile data to the clients using Rpc for rendering

However, the rendering function never gets called.

NetworkManager.cs

public override void OnServerReady(NetworkConnection conn)
{
    base.OnServerReady(conn);
    //Make sure they're all ready
    for (int i = RoomPlayers.Count - 1; i >= 0; i--)
    {
        if (!RoomPlayers[i].IsReady)
        {
            return;
        }
    }
    //Previously add SpawnTiles to OnServerReadied
    OnServerReadied?.Invoke(conn);
}

GameManager.cs

private void SpawnTiles(NetworkConnection conn)
{
    //Generate rawTiles beforehand
    Debug.Log(conn.isReady);
    Debug.Log("Entered spawn tiles");
    RpcSpawnTiles(rawTiles);
}

[ClientRpc]
public void RpcSpawnTiles(short[] rawTiles)
{
    Debug.Log("Client spawning tiles");
}

And this is my output when run on a host:

True
Entered spawn tiles

It appears to never enter the Rpc function :( Is there something super obvious that I'm missing? My GameManager does have a NetworkID attached to it

1

There are 1 answers

0
theseal53 On

There seemed to be a combination of things that I had to do to get it to work- I'm not entirely sure which one worked, but if you are having these problems, these are some of the things I had to check-

  1. Just basic code. Make sure that the appropriate things are being created on the client, host, or server
  2. Make sure that there aren't any race conditions, especially when working with changing scenes. You can call stuff from OnClientSceneChanged and OnServerSceneChanged
  3. As derHugo pointed out, I was not checking to see if my package could even be sent. It turns out that the maximum size that can be sent is 1200 bytes, while I was trying to send 100x100x4 bytes. This error didn't occur when I was testing on the host, only when there was an external client