Search code examples
c#unity-game-engineclientmultiplayer

Why is my Rpc function called when I asked for my Cmd function?


Here is my problem, when I run this code as a client,

private IEnumerator Start()

    {
        if (isServer)
        {
            Debug.Log("je suis le serveur");
            RpcAllowTeleportation();
        }
        else
        {
            Debug.Log("Je suis le client");
            CmdAllowTeleportations();
        }
    }

    [Command]
    public void CmdAllowTeleportations()
    {
        Debug.Log("C'est passé par la fonction CmdAllowTeleportation");
        if (!isLocalPlayer) { Teleportation.GetComponent<Teleport>().enabled = false; Debug.Log("cmd : it hasn't the authority"); }
        else { Debug.Log("cmd : It has the authority"); }
    }

    [ClientRpc]
    void RpcAllowTeleportation()
    {

        Debug.Log("C'est passé par la fonction RpcAllowTeleportation");
        if (!hasAuthority) { Teleportation.GetComponent<Teleport>().enabled = false; Debug.Log("rpc : it hasn't the authority"); }
        else { Debug.Log("rpc : It has the authority"); }
    } ##

I have these logs :

Mes logs

My logs are not at all consistent with the code ! Could you help me please ?

Thanks


Solution

  • Because Start is executed by the Server so it calls

    RpcAllowTeleportation();
    

    thus on all clients (so you) RpcAllowTeleportation is executed → you as client see the log of RpcAllowTeleportation.


    Vice versa you as client do never see the log from CmdAllowTeleportations since it is a [Command] which is only executed on the server → Only the server will see the log of CmdAllowTeleportations.