- Home /
Question by
siddharth3322 · Jun 25, 2018 at 12:32 PM ·
multiplayermultiplayer-networkingrpccommandunity multiplayer
How Client send info to Host in Unity Multiplayer
I want to send player name and other details from client to host device. I have setup game for maximum two players connection.
Here is the code that I have tried multiple times with different changes:
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;
StartCoroutine (SomeDelayForSendingPlayerDetails ());
}
// if (isServer)
// RpcSetRemotePlayerName (DataCollection.localPlayer.PlayerName);
// else
// CmdSetRemotePlayerName (DataCollection.localPlayer.PlayerName);
}
IEnumerator SomeDelayForSendingPlayerDetails ()
{
yield return new WaitForSeconds (0.5f);
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);
Debug.Log ("cmd remote player name: " + remotePlayerName);
}
At present Host can able to send information to other connected client but client can't able to send message to connected Host.
Please give me some help for this.
Comment
Your answer
Follow this Question
Related Questions
hi i need help with code 1 Answer
Interchange player names in Multiplayer game 1 Answer
UNET Network Discovery sends data with extra characters 0 Answers
Creating an array of player scores with Unity Multiplayer 0 Answers
Multiplayer - OnTriggerEnter2D Help.(Only working on Host, Not on Clients) 1 Answer