Search Unity

Third Party Photon Unity Networking - Username sync not working.

Discussion in 'Multiplayer' started by Divis10nStudios, May 19, 2021.

  1. Divis10nStudios

    Divis10nStudios

    Joined:
    Dec 26, 2017
    Posts:
    197
    First off: English isn't my first language, so I might not be able to explain in the best way possible.
    I was following a tutorial on YouTube, on syncing a username, which the player puts into a text input field. This username is then displayed as a 3d textmeshpro component on top of the player.
    I have got it working, kind of. The text successfully is displayed on the host client.

    However, I can't see the name that the other clients have chosen. This is what I see.

    After looking around in Unity, I found out that the username text input is outright empty on the second player.
    I tripled checked my code with the one from the tutorial, it's the exact same.

    public string username;

    ^^^ This gets the input from the text input field and stores it as a string.
    Code (CSharp):
    1. public class ProfileData
    2. {
    3.    public string username;
    4.  
    5.    public ProfileData()
    6.    {
    7.        this.username = "";
    8.    }
    9.  
    10.    public ProfileData(string u)
    11.    {
    12.       this.username = u;
    13.    }
    14. }
    It's then stored as this.

    I then use this code by calling AttempySync() in update

    Code (CSharp):
    1.    public void AttemptSync()
    2.    {
    3.        if(!photonView.IsMine) return;
    4.        photonView.RPC("SyncProfile", RpcTarget.All, Launcher.myProfile.username);
    5.    }
    6.  
    7.      [PunRPC]
    8.      private void SyncProfile(string playerUsername)
    9.      {
    10.          playerProfile = new ProfileData(playerUsername);
    11.          usernameText.text = playerProfile.username;
    12.      }
    If it helps, this is the original 2 tutorials I watched on the subject.
    - Part 1
    - Part 2.
    I am still a beginner to Photon so please try not to go ham on me :(
     
  2. toddkc

    toddkc

    Joined:
    Nov 20, 2016
    Posts:
    207
    I would set the player's PhotonNetwork.Nickname before joining a room, and then when a player joins the room (which you know by the callback OnPlayerJoinedRoom or something like that) you can set your local text for them to their nickname.
     
    Munchy2007 likes this.
  3. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    This is the way I do it.
     
  4. Divis10nStudios

    Divis10nStudios

    Joined:
    Dec 26, 2017
    Posts:
    197
    Thanks, I'll try that and report back here @toddkc @Munchy2007
     
  5. Divis10nStudios

    Divis10nStudios

    Joined:
    Dec 26, 2017
    Posts:
    197
    I fixed it guys! The problem wasn't explicitly that I wasn't using Photon.Nickname, it was that I was using it in the startgame() function, which meant that it only worked on the host,but now it works, and thanks for the nickname suggestion guys, it's much better, thanks guys!
     
    toddkc and Munchy2007 like this.