Hi,
I am having problems with simple text synchronisation.
I have button and textbox. I would like to change text in a textbox, after pressing a button and syncing it to all the clients. For now I can do it from server, but I would like to do it from client side as well.
Anyone knows simple sample for it? I am getting “Trying to send command for object without authority” all the time. I really tried it with local authority and without and can’t solve it.
Unity docs about authority are not clear enough to me…
Only the local player object can send commands to the server. The way I handle this problem is I have a component NetworkHUDBridge that the player object has that does something like this:
public class NetworkHUDBridge : NetworkBehaviour
{
private HUDManager m_HUD;
private void Start()
{
m_HUD = /*get a reference to the ui however you like*/;
}
[Client]
public void PostMessage(string msg)
{
CmdPostMessage(msg);
}
[Command]
private void CmdPostMessage(string msg)
{
m_HUD.ServerPostMessage(msg);
}
}
And then the HUDManagere looks something like:
public class HUDManager : NetworkBehaviour
{
//Get a reference to the local player whatever way is best for your app
private GameObject m_Player;
//This is called from the local UI when the player enters a message
public void OnMessageEntered(string msg)
{
m_Player.GetComponent<NetworkHUDBridge>().PostMessage(msg);
}
public void ServerPostMessage(string msg)
{
RpcPostMessage(msg);
}
[ClientRPC]
private void RpcPostMessage(string msg)
{
//Add the msg to the UI
}
}
Chain of event looks like this:
- Player enters message into chat window, name field, or whatever
- the UI sends the message to the NetworkHUDBridge component of the local player object
- NetworkHUDBridge (which is on the local player) sends a command to the server with the message
- NetworkHUDBridge (on the server side) sends the message to the UIManager on the server
- the UI sends an RPC to all clients with the message
- The UI on all the clients displays the message (or does whatever it needs to do)