- Home /
Interchange player names in Multiplayer game
I was trying to interchange players name so that both players can easily identify on each screen but at present this kind of thing happening, please check below image:
You can clearly see that names were not transferring. This kind of code, I have used and done multiple testing:
void Start ()
{
spriteRenderer = GetComponent<SpriteRenderer> ();
circleCollder = GetComponent<CircleCollider2D> ();
// StartCoroutine (ColorSwitcher ());
InitialTasks ();
}
private void InitialTasks ()
{
isAlive = true;
GameManager.Instance.IsPlayerWin = false;
int selectedTheme = Random.Range (0, ballSprites.Length);
spriteRenderer.sprite = ballSprites [selectedTheme];
trailRenderer.material = trailMaterials [selectedTheme];
destroyParticleObj.GetComponent<ParticleSystemRenderer> ().material = trailMaterials [selectedTheme];
hidePlayerName += OnTouchHidePlayerName;
if (!isLocalPlayer) {
// remote player
gameObject.tag = GameConstants.TAG_REMOTE_PLAYER;
} else {
// local player
// Debug.Log ("locally set player name: " + DataCollection.localPlayer.PlayerName);
playerNameText.text = DataCollection.localPlayer.PlayerName;
}
if (isServer)
RpcSetRemotePlayerName (DataCollection.localPlayer.PlayerName);
// else
// CmdSetRemotePlayerName (DataCollection.localPlayer.PlayerName);
}
[ClientRpc]
private void RpcSetRemotePlayerName (string remotePlayerName)
{
playerNameText.text = remotePlayerName + "@";
Debug.Log ("rpc remote player name: " + remotePlayerName);
}
[Command]
private void CmdSetRemotePlayerName (string remotePlayerName)
{
if (!isLocalPlayer) {
playerNameText.text = remotePlayerName + "#";
Debug.Log ("cmd remote player name: " + remotePlayerName);
}
}
Basically I want to interchange names of both players in 2 players multiplayer game. So please give me some advice into this.
It is good practice to initialise something in OnClientStart or OnStartLocalPlayer method, bcoz sometime this will called after the start..so it makes your logic dump...try if it helps
I have tried OnClientStart and few other similar methods too but result not get changed, do you think above all code is correct ?
Answer by LeeroyLin · Jun 22, 2018 at 02:52 AM
You can call a Cmd function for every local player, and it will execute on the server.
The server executes the cmd function, and in my function, it calls RPC function directly. So, every client will execute RPC function to set the correct name.
All this just like every player tell the server to broadcast the local player name. This is my script, I'm not sure what's your DataCollection.localPlayer.PlayerName means but hope this can help you complete your script.
By the way, when I test it, I have a strange problem, If I call the CMD function In OnStart function, it not work right, It seems every client to be ready need a few time, maybe. If you don't have that problem, you can use Cmd directly instead of my coroutine function.
private void InitialTasks ()
{
isAlive = true;
GameManager.Instance.IsPlayerWin = false;
int selectedTheme = Random.Range (0, ballSprites.Length);
spriteRenderer.sprite = ballSprites [selectedTheme];
trailRenderer.material = trailMaterials [selectedTheme];
destroyParticleObj.GetComponent<ParticleSystemRenderer> ().material = trailMaterials [selectedTheme];
hidePlayerName += OnTouchHidePlayerName;
if (!isLocalPlayer) {
// remote player
gameObject.tag = GameConstants.TAG_REMOTE_PLAYER;
} else {
// local player
// Debug.Log ("locally set player name: " + DataCollection.localPlayer.PlayerName);
playerNameText.text = DataCollection.localPlayer.PlayerName;
StartCoroutine("Delay");
}
}
IEnumerator Delay()
{
yield return new WaitForSeconds(.2f);
CmdSetRemotePlayerName (DataCollection.localPlayer.PlayerName);
}
[ClientRpc]
private void RpcSetRemotePlayerName (string remotePlayerName)
{
playerNameText.text = remotePlayerName + "@";
Debug.Log ("rpc remote player name: " + remotePlayerName);
}
[Command]
private void CmdSetRemotePlayerName (string remotePlayerName)
{
RpcSetRemotePlayerName (remotePlayerName);
}
Hope this can help you.
Still remote player not getting host player's name - check below image for more clearance:
Try to run your game in the real devece. In my case, this is work.
I have tried exactly same written code on two devices then also remove client can't able to get host player name.....
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
How Client send info to Host in Unity Multiplayer 0 Answers
Using animator with RPC 1 Answer
Multiplayer - OnTriggerEnter2D Help.(Only working on Host, Not on Clients) 1 Answer
hi i need help with code 1 Answer