- Home /
Rpc Can't be called on client - Problem with client, works on server
I'm making an rts and I'm trying to make it so when the builder goes to a structure and hits a trigger then the building spawns on the server. It works just fine on the host and the client receives it fine but when a client tries to build something, I get RpcCompleteStructure can't be called on Client.
OnTriggerEnter call this (structure being something in a local arraylist):
[ClientRpc]
private void RpcCompleteStructure(GameObject structure)
{
if (!isLocalPlayer)
{
return;
}
GameObject completedStructure = Instantiate(structure, structure.transform;
Destroy(structure); //spawns built structure and destroys construction site placeholder
NetworkServer.Spawn(completedStructure);
completedStructure .GetComponent<StructureScript>().CompleteBuild(); //completes structure, sets "built" variables
}
StructureScript CompleteBuild:
public void CompleteBuild()
{
GetComponent<UnityEngine.AI.NavMeshObstacle>().enabled = true;
built = true;
Destroy(gameObject.transform.GetChild(0).gameObject);
Destroy(gameObject.transform.GetChild(1).gameObject);
}
I'm new to multiplayer and would appreciate any help with it. Thank you
I'd actually prefer to have no placeholder structure for when it's spawned but other answers say that the player should call everything
Answer by fuz · Feb 07, 2017 at 01:51 AM
As you said, ClientRpc can only be call on server side, that's why it doesn't work when the client triggers it. Take a look at the documentation: https://docs.unity3d.com/Manual/UNetActions.html
Clients communicate with the server via Commands. The server communicates with the client via RPCs.
One workaround could be to have a Command (client to server) that calls the Rpc (server to client) you need.
Something like:
if(hasAuthority){
RpcTest(); //only server executes this part
}else{
CmdCallRpcTest(); //client tells the server to execute the Rpc
}
This could be your command
[Command]
void CmdCallRpcTest()
{
//Commands can only be sent from YOUR player object
RpcTest();
}
I figured it out on my own eventually and never though to check this question again, I basically did what you did. ClientRPC and Command are tricky to wrap your head around at first. Thanks for your help!