AnswerBun.com

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

[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.

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!

Related Questions

Resizing a tiledmap when using phaser

1  Asked on December 16, 2020 by sora

   

Prefab spawning issue

2  Asked on December 15, 2020 by usmaan-mahmood

   

Pause updating but keep rendering

1  Asked on December 5, 2020 by yosoku-denkai

     

How to make it so my cursor is not behind the UI?

0  Asked on December 3, 2020 by jacksonba90

   

Can’t submit facebook app for review

2  Asked on November 20, 2020 by moria

   

OpenGL Strange mesh when animating Assimp

1  Asked on October 26, 2020 by david-neves

     

2D Sidescroller Gravity

1  Asked on October 22, 2020 by pete-mavrelis

     

Can I “unmerge” merged meshes UE4?

0  Asked on September 22, 2020 by formatc2013

   

unity : How to delete unused assets?

0  Asked on September 6, 2020 by andio

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir