Question - Trying to send command for object without authority

Hello,

I am struggling two days now. So actually I created my own lobby where the player is able to join a match through the network. The player is able to create a server(client/host) and a second one is able to join it. When the second player joined, I want that both players getting a HUD activated. I tried it with an empty gameobject with the following script:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class ServerManager : NetworkBehaviour
{
    public static ServerManager instance;

    public GameObject CubeSelectionCanvas;

    void Awake()
    {
        instance = this;
    }

    [Command]
    public void CmdStartMatch()
    {
        RpcStartMatch();
    }

    [ClientRpc]
    public void RpcStartMatch()
    {
        CubeSelectionCanvas.SetActive(true);
    }

}

Furthermore the empty gameobject has an NetworkIdentity component attached with ‘Server only’.

The command is getting called by the second player, when he joins the match with: ServerManager.instance.CmdStartMatch();. Both players are getting instantiated with the NetworkManager and both have the ‘Local Player Authority’. Everytime when a client is trying to connect to the server, the following warning message is coming: ‘Trying to send command for object without authority’.

Testingwise I also added a script to my player and tried this:

public override void OnStartLocalPlayer()
{
    ServerManager.instance.CmdStartMatch();
}

It is working for the Client/Host, but does not work for the normal client.

Does someone have a solution for it? Thanks in advance.

Sander

You unfortunatly cannot call commands on Netobjects where you have no authority. It is working for your Host because he has authority.
For your case, if I read correct, you could simply activate your UI when the second player is started, right?

public class Player : NetworkBehaviour
{
    public static HashSet<Player> ActivePlayers = new HashSet<Player>();
    public override void OnStartClient()
    {
        base.OnStartClient();
        ActivePlayers.Add(this);
        if (ActivePlayers.Count == 2)
            ServerManager.instance.CubeSelectionCanvas.SetActive(true);
    }
    protected void OnDestroy()
    {
        ActivePlayers.Remove(this);
    }
}
2 Likes

Thank you very much @PhilippG - that is working!

Small point, no need to call base.OnStartClient, the virtual function is empty
(NetworkBehaviour source: https://goo.gl/R17vLx)

You never know what the next update brings :wink:

1 Like