UNET Client Cant Send Server Commands, Host Can

Sebastian Thomas Edward Lawe picture Sebastian Thomas Edward Lawe · Sep 3, 2015 · Viewed 9k times · Source

So as the title suggests, I'm having a problem where commands being sent by the client are not triggered.

The basic functionality I'm trying to get working is that when an enemy is in front of the player and I click, that player will be momentarily stunned. Works fine if I'm host, and both sides perfectly register.

If I'm playing as client, I get to the point where the command "should" be sent, but I notice I get a warning that says "Trying to send command for non-local player". As well, nothing happens on either end. Obviously I must be doing something wrong client side, but have no idea what other way to go about this.

The "problem" code.

            if (Input.GetButtonDown("Fire1")) {
            Ray ray = new Ray(transform.position, transform.forward);
            RaycastHit hit = new RaycastHit();
            Physics.Raycast(ray, out hit, 20f);
            if (hit.transform != null) {
                if (hit.rigidbody != null) {
                    PlayerController controller = hit.rigidbody.GetComponent<PlayerController>();
                    if (controller != null) {
                        if (!controller.stunned) {
                            // Send the stun command to the server
                            controller.CmdStun();
                        }
                    }
                }
            }
        }

The method calls

[Command]
public void CmdStun() {
    // Report that the stun was sent
    UnityEngine.Debug.Log("Stun Sent");
    RpcStun();
}

[ClientRpc]
public void RpcStun() {
    // Activate the stun timer.
    stunTimer.Start();
    // Track the players original color.
    normalColor = manager.color;
    // Make the players bot look like its shut down.
    manager.InitiateColorChange(Color.black);
    // Other code will check against this before trying to send another stun command.
    stunned = true;
}

Edit: Upon request heres the two scripts in their entirety.

http://pastebin.com/mr4A9ZgH

http://pastebin.com/Qg0AjCCD

Player configuration in unity:

https://gyazo.com/400c5b3a95c1a58f9b6e930b0c3c853b

https://gyazo.com/c17a7317767a00e2150ff34b03a03e8f

https://gyazo.com/322731aefbe69f9567d2b395523b8f2a

Full Warning Message

Trying to send command for non-local player. UnityEngine.Networking.NetworkBehaviour:SendCommandInternal(NetworkWriter, Int32, String) PlayerController:CallCmdStun() ObjectInteractor:Update() (at Assets/Scripts/ObjectInteractor.cs:58)

Answer

马佳伟 picture 马佳伟 · Jun 2, 2016

I guess you are calling a command on a gameobject you spawn by networkserver or the object has existed the scene already. Anyway, the object is not a playerprefab that you assign it in the NetworkManager's SpawnInfo.

So, for those Object that 'Trying to send command for non-local player'. You need to give their NetworkIdentity a 'Authority' which enable them to call [Command].And to give them 'Authority' you should use

NetworkIdentity.AssignClientAuthority(connectionToClient).

And, as a fact NetworkIdentity.AssignClientAuthority may only call on a server. You need an other [Command] on 'Player' to 'AssignClientAuthority'.

More details are here:

Unity UNET - calling [Command] outside of player object

(I'm a Chinese, so maybe I didn't express myslef clearly, hope you can understand.)