Hi all!
I have simple script:
public class Player: NetworkBehaviour {
public void OnMouseDown()
{
CmdColor();
}
[Command]
public void CmdColor()
{
RpcColor(Random.ColorHSV());
}
[ClientRpc]
public void RpcColor(Color color)
{
GetComponent<MeshRenderer>().material.color = color;
}
}
it works on the server, but on client it works only for local player. If i click on client to remote players, in console i have warning “Trying to send command for object without authority”. I think it due security. In Unity docs written “For security, Commands can only be sent from YOUR player object”.
I changed my script:
public class Player: NetworkBehaviour {
public static Player Local { get; private set; }
public override void OnStartLocalPlayer()
{
base.OnStartLocalPlayer();
if (Local == null)
Local = this;
}
public void OnDestroy()
{
if (Local == this)
Local = null;
}
public void OnMouseDown()
{
Player.Local.CmdColor(netId);
}
[Command]
public void CmdColor(NetworkInstanceId id)
{
NetworkServer.FindLocalObject(id).GetComponent<Player>().RpcColor(Random.ColorHSV());
}
[ClientRpc]
public void RpcColor(Color color)
{
GetComponent<MeshRenderer>().material.color = color;
}
}
This script works perfectly, but i not shure, that is the best way?