How Client send info to Host in Unity Multiplayer

by Siddharth   Last Updated June 28, 2018 17:13 PM

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

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

Basically I want to interchange names of both players in 2 players multiplayer game. So please give me some advice into this.



Related Questions




Delayed linear interpolation for networking

Updated June 13, 2017 23:13 PM

Creating a master server for Unity game (Unity 2017)

Updated September 08, 2017 15:13 PM