0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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;     
}
\$\endgroup\$
  • \$\begingroup\$ thanks for your reply :) how to set player name to only our remote player? Not all clients that joined the server.... \$\endgroup\$ – Siddharth Jul 6 '18 at 10:45
  • \$\begingroup\$ you can use if(!isLocalPlayer) to make sure it is not the local player. \$\endgroup\$ – Justin Markwell Jul 6 '18 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.