Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question - Trying to send command for object without authority

Discussion in 'Multiplayer' started by Sander1991, Aug 2, 2016.

  1. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class ServerManager : NetworkBehaviour
    6. {
    7.     public static ServerManager instance;
    8.  
    9.     public GameObject CubeSelectionCanvas;
    10.  
    11.     void Awake()
    12.     {
    13.         instance = this;
    14.     }
    15.  
    16.     [Command]
    17.     public void CmdStartMatch()
    18.     {
    19.         RpcStartMatch();
    20.     }
    21.  
    22.     [ClientRpc]
    23.     public void RpcStartMatch()
    24.     {
    25.         CubeSelectionCanvas.SetActive(true);
    26.     }
    27.  
    28. }
    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:
    Code (CSharp):
    1. public override void OnStartLocalPlayer()
    2. {
    3.     ServerManager.instance.CmdStartMatch();
    4. }  
    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
     
  2. PhilippG

    PhilippG

    Joined:
    Jan 7, 2014
    Posts:
    257
    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?
    Code (CSharp):
    1. public class Player : NetworkBehaviour
    2. {
    3.     public static HashSet<Player> ActivePlayers = new HashSet<Player>();
    4.     public override void OnStartClient()
    5.     {
    6.         base.OnStartClient();
    7.         ActivePlayers.Add(this);
    8.         if (ActivePlayers.Count == 2)
    9.             ServerManager.instance.CubeSelectionCanvas.SetActive(true);
    10.     }
    11.     protected void OnDestroy()
    12.     {
    13.         ActivePlayers.Remove(this);
    14.     }
    15. }
     
    isidro02139 and Sander1991 like this.
  3. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    Thank you very much @PhilippG - that is working!
     
  4. isidro02139

    isidro02139

    Joined:
    Jul 31, 2012
    Posts:
    72
    Small point, no need to call base.OnStartClient, the virtual function is empty
    (NetworkBehaviour source: https://goo.gl/R17vLx)
     
  5. PhilippG

    PhilippG

    Joined:
    Jan 7, 2014
    Posts:
    257
    You never know what the next update brings ;-)
     
    isidro02139 likes this.