Search Unity

Trying to send command object without authority

Discussion in 'Editor & General Support' started by akbararviano, Sep 14, 2019.

  1. akbararviano

    akbararviano

    Joined:
    Sep 12, 2019
    Posts:
    1
    Hello , im trying to implement a simple chat in TPS multiplayer game , when playing as host , the chat works perfect , but in client it tells " Trying to send command object without authority " , i've looked on every answer and none of them fix my problem , can anyone help me ? thanks :).

    Heres the code :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Networking;
    6.  
    7. public class ChatScript : NetworkBehaviour
    8. {
    9.  
    10.     public GameObject ChatPanel;
    11.     public GameObject TextObject;
    12.     public InputField Input_Field;
    13.     [SyncVar]public int maxMessage = 25;
    14.     [SerializeField]
    15.      List<Message> messagelist = new List<Message>();
    16.  
    17.  
    18.     void Update()
    19.     {
    20.  
    21.         Input_Field = GameObject.Find("InputField").GetComponent<InputField>();
    22.         ChatPanel = GameObject.Find("ChatPanel");
    23.         if (Input.GetKeyDown(KeyCode.Return))
    24.         {
    25.             if (Input_Field.text != "")
    26.             {
    27.                 if(isServer)
    28.                 {
    29.                 RpcSendMessageToChat(Input_Field.text);
    30.                 Input_Field.text = "";
    31.                 }
    32.      
    33.                 else
    34.                 {
    35.                     CmdEnviar(Input_Field.text);
    36.                     Input_Field.text = "";
    37.                 }
    38.             }
    39.         }
    40.     }
    41.  
    42.     [Command]
    43.  
    44.     void CmdEnviar(string text)
    45.     {
    46.         RpcSendMessageToChat(Input_Field.text);
    47.     }
    48.  
    49.     [ClientRpc]
    50.     public void RpcSendMessageToChat(string text)
    51.     {
    52.         if(messagelist.Count >= maxMessage)
    53.         {
    54.             Destroy(messagelist[0].TextObject.gameObject);
    55.             messagelist.Remove(messagelist[0]);
    56.         }
    57.         Message newMessage = new Message();
    58.         newMessage.text = text;
    59.         GameObject newText = Instantiate(TextObject, ChatPanel.transform);
    60.         newMessage.TextObject = newText.GetComponent<Text>();
    61.         newMessage.TextObject.text = newMessage.text;
    62.         messagelist.Add(newMessage);
    63.     }
    64.  
    65.  
    66.     [System.Serializable]
    67.  
    68.         public class Message
    69.         {
    70.         [SyncVar]public string text;
    71.         public Text TextObject;
    72.         }
    73.  
    74.  
    75. }
    and for the full error code :
    Trying to send command for object without authority.
    UnityEngine.Networking.NetworkBehaviour:SendCommandInternal(NetworkWriter, Int32, String)
    ChatScript:CallCmdEnviar(String)
    ChatScript:Update() (at Assets/Script/ChatScript.cs:35)
     
    Last edited: Sep 14, 2019