How Client send info to Host in Unity Multiplayer

Game Development Asked on January 3, 2022

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);

public override void OnStartLocalPlayer ()
    base.OnStartLocalPlayer ();
    CmdSetRemotePlayerName (DataCollection.localPlayer.PlayerName);

private void RpcSetRemotePlayerName (string remotePlayerName)
    playerNameText.text = remotePlayerName;
    Debug.Log ("rpc remote player name: " + remotePlayerName);

private void CmdSetRemotePlayerName (string remotePlayerName)
    RpcSetRemotePlayerName (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.

One Answer

Looks like your setup should work. Make sure you have a network identity and you are a local player.

Add a debug log in InitialTasks and if it does not return the playerNameText properly then make sure that PlayerNameText element is active when you set it.

else {
    // local player
       playerNameText.text = DataCollection.localPlayer.PlayerName;
       Debug.Log (playerNameText.text);

If the element is inactive you wont set it properly so possibly set it then hide it and see if that works.

Another way to sync up variables is to use a syncvar.

In this example the variable is syncing the name on start as the value changes.

[SyncVar(hook = "SyncName")]
public string clientName="";

private void Start()
{   //as I change the name it will sync
    clientName = "Justincredible";

void SyncName(string name) 
    playerNameText.text = name;     

Answered by Justin Markwell on January 3, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP