So I am trying to set up a system where I can remotely control my game from either the host machine, or a connected client on another machine (basically allowing 2 different people on 2 different machines to control the player’s combat units in my game) and right now my code is working when I control the game from the host, but it’s not working if I try to control the game from the client.
When I tell a unit in the game to move on the host, the unit moves to the correct tile, and on the client you can see the action is executed correctly there as well. But if I try to tell a unit to move on the client, I get the warning “Trying to send command to object without authority”.
To accomplish what I have so far, I have a script that controls unit movement, and on mouseClick, it executes a [Command] function, that in turn calls a [ClientRpc] function. The hope of doing this was that a player would click on a tile, and that would send the command to the server, where the server could broadcast the ClientRpc function basically making all the clients execute the same function at the same time.
But like I said, it only works correctly on the Host and doesn’t work when I’m playing on a client connected to the host.
Any help on this would be appreciated.
My current code looks something like this
//PlayerUnitController gameobject has a network identity set to //Local Authority
public class PlayerUnitController : NetworkBehaviour
{
void Update()
{
if (Input.GetMouseButtonDown(0))
{
CastRay();
}
}
void CastRay()
{
Ray ray = topDownCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
if (hit.collider.gameObject.tag == "tile")
{
CmdMsgRayHitToServer(hit.collider.gameObject);
}
}
}
[Command]
void CmdMsgRayHitToServer(GameObject _hit)
{
RpcSyncMoveWithAllClients(_hit);
}
[ClientRpc]
void RpcSyncMoveWithAllClients(GameObject hitGameObject)
{
//perform unit move code.....
}
}
built-in unity math types (Vector3, Quaternion, etc)
NetworkIdentity
NetworkInstanceId
NetworkHash128
GameObject with a NetworkIdentity component attached
The tile gameobject that I’m passing through the command / Rpc functions is a gameobject with Network Identity component attached, so that’s not the problem.
The code works perfectly on the host and I can see the other client running the game stay in sync with unit moves made on the host. On the Host computer, I tell a unit to move from tile1 to tile12 and on the host the unit moves, and on the client machine I see the unit move correctly.
So clearly the code is working, it’s just that I’m only apparently allowed to control the units from the host machine, and the 2nd player connected as just a client “doesn’t have authority”.
So is there something I need to do to dynamically transfer authority depending on who is performing the actions?