I try to send a message from the server to the clients, only the local player should display the message, but isLocalPlayer doesn’t work in the RpcPrintDebug() method. I can see the debug message in the console in RpcPrintDebug(), but not the message in the Text object.
This is my PlayerController:
public class PlayerController : NetworkBehaviour {
void Update ()
{
if ( !isLocalPlayer ) {
return;
}
if (Input.GetKeyDown(KeyCode.M)) {
CmdAddMessage("Key M pressed!");
}
}
[Command]
public void CmdAddMessage(string message)
{
GameDataManager gdm = GameObject.FindGameObjectWithTag("GameDataManager").GetComponent<GameDataManager>();
gdm.CmdAddMessage (message);
}
[ClientRpc]
public void RpcPrintDebug(string message)
{
Debug.Log (message);
if ( !isLocalPlayer ) {
return;
}
Text debugText = (Text)GameObject.Find ("DebugText").GetComponent<Text> ();
debugText.text += message + "
";
}
}
And this is my GameDataManager:
public class GameDataManager : NetworkBehaviour {
[Command]
public void CmdAddMessage(string message)
{
PlayerController player = GameObject.FindGameObjectWithTag ("Player").GetComponent<PlayerController> ();
player.RpcPrintDebug (message);
}
}
I’m not sure if you understand your event flow here. You may have several players in your game. Each player has a player objects with your playercontroller script. Each of those can send out the “CmdAddMessage” command to their playercontroller representation on the server. Over there you then call “CmdAddMessage” on the GameDataManager.
Inside the GameDataManager you pick the first player object that is tagged “Player” and you send the RPC “RpcPrintDebug” only to this one playerobject. Of course this would always be the same playerobject and most likely is the playerobject of the server since that’s usually the first playerobject created. The RPC is then executed on all clients but only on that one playerobject
If you want to react to the actual player that originally send the command you have to communicate who actually called the command from your playerobject to your GameDataManager. Something like this:
public class GameDataManager : NetworkBehaviour
{
public void CmdAddMessage(PlayerController player, string message)
{
player.RpcPrintDebug (message);
}
}
Of course you would call that method like this from the player controller:
gdm.CmdAddMessage (this, message);
Now you will send the RpcPrintDebug back to the player object that has send the command. Of course the RPC will be executed on all clients but only on that one player object.
Note that the “CmdAddMessage” inside GameDataManager should not be a “Command”. Commands are meant to be called by clients which they most likely can’t because they do not have authority over it.