- Home /
Rpc Methods are not executed in remote client, unless Unity Editor is hosting the match.
I'm currently developing a cardgame for android, you can think of it like Hearthstone.
The problem I'm facing lies with the online component of the game. I can connect two clients with another, but the RpcMethod code is not executed in the remote client.
For now, I want each player to have a different view, each player should see his own hand and the amount of cards the opponent holds in his hands. The way I chose to do this was to make each Player object hold the player's hand as a childobject, and a gameobject which can display how many cards the opponent holds (just cardbacks, no actual card data).
To only show each player their own hand, this is the code that is executed at the first frame in Update() (so that it only executes after both players joined):
//client find playerOBJs
clientController.RpcFindPlayers();
//hide other player's view from each player
clientController.RpcHideOtherPlayer();
//Testing this
clientController.RpcColorMeSurprised();
And here is what those methods look like:
[ClientRpc]
public void RpcFindPlayers()
{
GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
if (isServer)
{
myPlayer = players[0].GetComponent<PlayerController>();
otherPlayer = players[1].GetComponent<PlayerController>();
}
else
{
otherPlayer = players[0].GetComponent<PlayerController>();
myPlayer = players[1].GetComponent<PlayerController>();
}
}
[ClientRpc]
public void RpcHideOtherPlayer()
{
otherPlayer.transform.localScale = Vector3.zero;
}
//for testing purposes
[ClientRpc]
public void RpcColorMeSurprised()
{
DebugConsole.Log("ClientController.ColourMe()", "error");
SpriteRenderer colourMe = myPlayer.transform.Find("ColorMeSurprised").GetComponent<SpriteRenderer>();
if (isServer)
{
colourMe.color = Color.blue;
}
else
{
colourMe.color = Color.red;
}
DebugConsole.Log("ClientController.Recoloured!", "error");
}
What the methods do: RpcFindPlayers() finds each of the players (in form of their PlayerController Script) on each server, and saves them for later use.
RpcHideOtherPlayer() reduces the scale of the other player to 0 in each client, meaning that the other player (meaning THEIR hand) is invisible.
RpcColorMeSurprised() changes the colour of a little sprite on each players hand, blue on the hosting client, and red on the guest client. (this is for my own testing purposes)
When I host the game (Create Internet Match) in the editor, and connect through a client, everything works perfectly as it should (see image)
But usually, when I try to connect two clients that are both not the unity editor, or when I host the game in the client, and connect through the editor, the functions mentioned above are not executed in the remote client!
I say usually, because this isn't even always the case. Sometimes, apparently at random, it DOES work if I connect two clients with another. Also, if I make a new build (new folder and the build has a different name), two non-editor clients do reliably connect with each other, and all code is executed properly, but after launching it a few times it simply doesn't work anymore...
I'm at a complete loss at this time, I have no idea what else I could try, I suspect it might have something to do with unity's multiplayer services, but I have no prior experience so I don't really know...
I'd be very grateful for any help I can get, and I can provide any additional information that you might need.
After testing around some more, and with help from a colleague, I figured out the root of the problem: The clients are not all connected, or done loading the scene (I'm entering the game from a lobby scene) when the Rpc methods are called, so the remote client cannot catch the call in time.
If I introduce a three second delay before doing anything, the remote client can load in, and execute the methods.
Surely though there must be a better way to do this than the three second delay? I had already tried things like OnPlayerConnected to tell the server when the client is ready, but that didn't work either...
If anyone has a good solution for this, please share :)
Your answer
Follow this Question
Related Questions
How to use an RPC to send an animation over the network? 2 Answers
RPC Parameter Optimization 1 Answer
When connected to a server, can other players access my static variables and/or public variables? 2 Answers
Photon Networking: RPC call doesn't work over other clients 0 Answers
Photon variable synchronization 1 Answer