Players' usernames not syncing properly in Unity scene!

I am hoping to get some help with this, because I'm completely stuck! So far in my Unity game, I have a login system and a database where I am storing user accounts (I am using Playfab in this case to store player info), and I am using Photon Pun2 in order to create my scene. I have been testing two different players spawning into the room, and I am trying to get their usernames to sync about their heads.

On each player's individual screen, the player's own username shows up perfectly. But, I can not for the life of me understand why the other player's username won't show on others' screens. Here is my basic code setup:
public class PlayerController : MonoBehaviour
 {
 
     private PhotonView photonView;
 
     public Text display;
     public string username;
 
     private void Start()
     {
         photonView = GetComponent<PhotonView>();
     }
 
     void OnDisplayName(GetAccountInfoResult result)
     {
     if (photonView.IsMine)
         {
             username = result.AccountInfo.Username;
     }
     }
 
     [PunRPC]
     public void getDisplayName()
     {
         GetAccountInfoRequest request = new GetAccountInfoRequest();
         PlayFabClientAPI.GetAccountInfo(request, OnDisplayName, OnErrorDisplay);
         
     }
 
     void OnErrorDisplay(PlayFabError error)
     {
         Debug.Log("Error with display name");
     }
 
     private bool playercount = false;
 
     private void FixedUpdate()
     {
         if (photonView.IsMine)
         {
             if (PhotonNetwork.CurrentRoom.PlayerCount == 2 && playercount == false)
             {
                 playercount = true;
                 photonView.RPC("getDisplayName", RpcTarget.AllBuffered);
             }
 
         display.text = username;
         Debug.Log(username + " is in the game.");
     }
 
     public virtual void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
     {
         if (stream.IsWriting)
         {
             stream.SendNext(username);
         }
         else if (stream.IsReading)
         {
             username = (string)stream.ReceiveNext();
         }
     }
 
 }

Another thing, in the FixedUpdate() function, where I have the debug line...
Debug.Log(username + " is in the game.");

it IS recognizing that there are two players. But, it still isn't printing out the other username. The debug log will show something like this:
"Player1 is in the game."
"  is in the game."

And that's all it prints out, meaning it recognizing that two players are present, but still fails to grab the other player's username. Any help with this is much appreciated. I have spent hours switching code around and to no avail. Idk what I'm missing.
Sign In or Register to comment.