Search Unity

"Trying to send command for object without authority." On playerprefab with network identity

Discussion in 'Multiplayer' started by InCodeWeTrust, Apr 5, 2018.

  1. InCodeWeTrust

    InCodeWeTrust

    Joined:
    Oct 26, 2017
    Posts:
    4
    Hello =),

    I want to call a command but it tells me "Trying to send command for object without authority."

    The Chat.cs script is on my playerprefab that is automatically spawned by the network on start.

    The script is directly on the player prefab and not on a child.

    The playerprefab of course has a network identity with local player authority checked.

    I really do not know how to handle the situation, this issue is stopping my project :/.

    Any help is welcome =).

    Thanks a lot.


    Code (CSharp):
    1. using UnityEngine.Networking;
    2. using UnityEngine.UI;
    3. using UnityEngine;
    4. public class Chat : NetworkBehaviour {
    5.    public float messageLifeTime;
    6.    private GameObject chatHolder;
    7.    private Text chatBox;
    8.    private InputField inputField;
    9.    private List<string> chatMessages;
    10.    private List<float> apparitionDates;
    11.  
    12.    private void Awake()
    13.    {
    14.        chatMessages = new List<string>();
    15.        apparitionDates = new List<float>();
    16.    }
    17.    void Start () {
    18.        chatHolder = GameObject.FindGameObjectWithTag("ChatHolder");
    19.        chatBox = GameObject.FindGameObjectWithTag("ChatBox").GetComponent<Text>();
    20.        inputField = GameObject.FindGameObjectWithTag("ChatInput").GetComponent<InputField>();
    21.        inputField.onEndEdit.AddListener(AddMessageFromChatToServer);
    22.      }
    23.    
    24.     public void AddMessageFromChatToServer(string message)
    25.     {
    26.        CmdAddMessageFromChat(message, PlayerPrefs.GetString("NickName")); //Error is at this line
    27.     }
    28.     [Command]
    29.     public void CmdAddMessageFromChat(string message, string name)
    30.     {
    31.        RpcAddMessageFromChat(message, name);
    32.     }
    33.     [ClientRpc]
    34.     public void RpcAddMessageFromChat(string message, string name)
    35.     {
    36.        if (!CheckCommands(message))
    37.        {
    38.           AddMessageToDisplay(name + ": " + message);
    39.           apparitionDates.Add(Time.time);
    40.        }
    41.     }
    42.     void AddMessageToDisplay(string message)
    43.     {
    44.        Debug.Log("displayed");
    45.     }
    46.    bool CheckCommands(string message)
    47.    {
    48.       return false;
    49.    }
    50. }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You are probably calling AddMessageFromChatToServer on all player objects in the scene instead of just the player object that represents that single client. The way you have Start and AddMessageFromChatToServer written they will be run on any client for any player gameobject. You need to restrict methods that should only be run on the client's player object. Try checking for isLocalPlayer like below to check if this fixes your error, but I'd still clean up your Start method to prevent running things you only want on the client that owns the object from running on every single client.

    Code (csharp):
    1.  
    2. public void AddMessageFromChatToServer(string message)
    3. {
    4.     if (isLocalPlayer)
    5.     {
    6.         CmdAddMessageFromChat(message, PlayerPrefs.GetString("NickName")); //Error is at this line
    7.     }
    8. }
    9.  
     
    InCodeWeTrust likes this.
  3. InCodeWeTrust

    InCodeWeTrust

    Joined:
    Oct 26, 2017
    Posts:
    4
    First thanks a lot for your quick answer =)

    I wrote this :
    Code (CSharp):
    1.     public void AddMessageFromChatToServer(string message)
    2.     {
    3.         if (isLocalPlayer) CmdAddMessageFromChat(message, PlayerPrefs.GetString("NickName"));
    4.         else Debug.Log("IsNotLocalPlayer");
    5.     }
    But it logged IsNotLocalPlayer :/
    The thing is I have other scripts on my player prefab that are working on network :'(

    Edit: Adding a screen of my prefab
     

    Attached Files:

    Last edited: Apr 5, 2018
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    So you're not being very clear. In a networked game you generally have at least 1 client and 1 server or host. Are you getting this debug message on the server or the client? How many player objects are in the scene at that point?

    The way your if/else works is you should successfully send the Command on the player object the client owns, and you will get a number of debug messages equal to the total number of player objects in the scene minus 1. That's why I was saying you need to still clean up your Start method, because I don't think you want to add a listener for your input field on player gameobjects that represent other clients like you're currently doing, instead just the object that represents that local client.
     
    InCodeWeTrust likes this.
  5. InCodeWeTrust

    InCodeWeTrust

    Joined:
    Oct 26, 2017
    Posts:
    4
    Yeah sorry I forgot this point =)

    In a situation with
    1 Host, 2 other clients.
    The host is able to send messages in the chat.
    The clients aren't able to send messages, and the debugline is called twice.
    So if I understand well what you are saying me, I should add my EventListener at runtime directly on the inputfield through an other script, and not in my Chat.cs ?

    Thanks again for your answers, it's pleasant to have quick answers as a beginner =)

    Edit: If I change the Start method with OnStartLocalPlayer, would it change anything too ?
    Edit bis : the input field is in the hierarchy in editor mode, and the player isn't
     
    Last edited: Apr 5, 2018