RPC and Cmd don't work like should?

Hello there.

So I got simple task here, I want to change scene when someone press a button, even client. So here is my code which both of players have attached:

    [Command]
    public void CmdRestartLevel() {
        Debug.Log("cmdCalled");
        RpcRestartLevel();
    }

   [ClientRpc]
    void RpcRestartLevel()
    {
        Debug.Log("RpcCalled");
    networkM.ServerChangeScene("scene");
}

It works for host, but when I press it on client it says “RPC function called on client”

Let’s point out few things:
This piece of code is in script which is attached to GameController GameObject.
GameController has Network Identity with Local Player Authority
Canvas has Network Identity with Local Player Authority
Button (from Canvas) which is calling "CmdRestartLevel() has Network Indentity with Local Player Authority.

Anyone who could help or explain it a bit? I was looking for solution but AFAIK that’s how you’re supposed to call Rpc from client (via Cmd).

Best regards.

What method is the client calling? Clients should use the Command method and Server should use the Rpc.

@TwoTen Thanks for reply.

Correct me if I am wrong, but I was thinking that you can put Cmd and both host and client can use it to execute this command. According to BluetoothBoy’s reply from https://forum.unity3d.com/threads/invoke-method-on-players-command-vs-clientrpc.335780/ his code should work, shouldn’t it?

So basicaly when you press UI button you get “CmdRestartLevel()” called. It’s same button for client and host. When I press it at host, it works. Both client and host are restarting level. When I press it as client it says that “RPC was called from client” (which shouldn’t happed according to BluetoothBoy’s answer)

I changed my code a bit. Now it’s look like this and clicking UI button is executing “RestartLevel()”.

 [Command]
    public void CmdRestartLevel() {
        Debug.Log("cmdCalled");
        networkM.ServerChangeScene("scene");
    }
    [ClientRpc]
    public void RpcRestartLevel()
    {
        Debug.Log("RpcCalled");
        networkM.ServerChangeScene("scene");
    }
    public void RestartLevel() {
        if (isServer) {
            RpcRestartLevel();
        }
        else {
            CmdRestartLevel();
        }
    }

And now I get warning (not an error) that “Trying to send command for object without authority” when client tries to press button, but again, it’s working when host does so.

Warning is pointing at line where CmdRestartLevel(); is called from RestartLevel() function:

  public void RestartLevel() {
        if (isServer) {
            RpcRestartLevel();
        }
        else {
            CmdRestartLevel();
        }
    }
}

But what object doesn’t have authority? GameController, Canvas, Buttons have Player Authority set in editor.

Best regards.

I don’t think you should be manually changing scenes when using NetworkManager. It supposed to be handled by NetworkServer. All you need to do is to change it on server, I think. (never tried this though)

1 Like

Yeah, I found it already, but thanks for answer.