Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  5. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Unity Multiplayer UNet UI Player selection

Discussion in 'Connected Games' started by GreeniPeach, Jun 11, 2018.

  1. GreeniPeach

    GreeniPeach

    Joined:
    Sep 4, 2017
    Posts:
    5
    Hi,

    I´m trying to set up a simple UI, where 6 Players can pick there position.

    If one of the clients clicks a button the place will be assigned to him and the button should be destroyed.

    It works for the host. But on my client it won't work. I always get this error:
    Trying to send command for object without authority.

    And I can´t figure out how to set the authority to the player who clicked the button. Can anyone please help me.

    Code (CSharp):
    1.     public List<GameObject> buttons= new List<GameObject>();
    2.  
    3.     public void Button(int i)
    4.     {
    5.         if (isServer)
    6.         {
    7.             RpcUpdatePlayerActive(i);
    8.            
    9.         }else
    10.         {
    11.  
    12.             CmdSetPlayerActive(i);
    13.         }
    14.     }
    15.    
    16.     [Command]
    17.     public void CmdSetPlayerActive(int player)
    18.     {
    19.        
    20.         Debug.Log("CMD called");
    21.         RpcUpdatePlayerActive(player);
    22.     }
    23.  
    24.     [ClientRpc]
    25.     public void RpcUpdatePlayerActive(int player)
    26.     {
    27.         Debug.Log("RPC called");
    28.         switch (player)
    29.         {
    30.             case 1:
    31.                 GameManager.Player1Ready = true;
    32.                 Debug.Log("Player " + player + "ready");
    33.                 Destroy(buttons[player-1]);
    34.                 break;
    35.             case 2:
    36.                 GameManager.Player2Ready = true;
    37.                 Debug.Log("Player " + player + "ready");
    38.                 Destroy(buttons[player-1]);
    39.                 break;
    40.             case 3:
    41.                 GameManager.Player3Ready = true;
    42.                 Debug.Log("Player " + player + "ready");
    43.                 Destroy(buttons[player-1]);
    44.                 break;
    45.             case 4:
    46.                 GameManager.Player4Ready = true;
    47.                 Debug.Log("Player " + player + "ready");
    48.                 Destroy(buttons[player-1]);
    49.                 break;
    50.             case 5:
    51.                 GameManager.Player5Ready = true;
    52.                 Debug.Log("Player " + player + "ready");
    53.                 Destroy(buttons[player-1]);
    54.                 break;
    55.             case 6:
    56.                 GameManager.Player6Ready = true;
    57.                 Debug.Log("Player " + player + "ready");
    58.                 Destroy(buttons[player-1]);
    59.                 break;
    60.         }
    61.  
    62.     }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    3,954
    Your problem is exactly as specified in the error message. A Command can only be sent by the client on an object the player either has been assigned authority for, or on that client's Player GameObject (even if you're using server authority for the Player object).

    So you'd either want to move this functionality to a script attached to the Player GameObject, or switch to using Unet Messages which aren't actually associated with specific GameObjects or care about object authority.

    I'd go the Message route.

    https://docs.unity3d.com/Manual/UNetMessages.html
     
    GreeniPeach likes this.