Search Unity

Help Wanted Photon 2, Player username can't sync, can't show others username

Discussion in 'Multiplayer' started by andreas3739, Oct 19, 2020.

  1. andreas3739

    andreas3739

    Joined:
    Jul 1, 2020
    Posts:
    14
    Code (CSharp):
    1.      
    2. if(photonView.IsMine){
    3.                 nameText.text = PhotonNetwork.NickName;
    4.         }
    5.         else {
    6.             nameText.text = GetComponent<PhotonView>().Owner.NickName;
    7.         }
    8.     }
    9.  
    I try this code, but it only did not show another players name, except my own name. Same goes to the other player it only show (his) name but others did not show.

    I have been trawling the whole thing the whole week , sadly to post a new threat. I hope I could find the solution at this post

    I have put photon view at my player, I watch and follow some tutorials but still did not found any solutions.
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,686
    If you didn't update yet, get the latest PUN 2 version please. We started caching Owner and improved handling for ownership transfer (not used here but slightly related).

    The Owner.NickName can be use for all views. You need to set PhotonNetwork.NickName to some string to sync this. If you set it before connecting or joining a room, the players in the room will know immediately when someone joins.
     
    andreas3739 likes this.
  3. andreas3739

    andreas3739

    Joined:
    Jul 1, 2020
    Posts:
    14

    Oh I see, is that man that I am not updated ? But I think I just get PUN2 in unity store.

    I set Photon Nickname after joined a lobby, and. Should that be a problem? Basically ConnectUseSetting -> Joined Lobby -> Write name -> Create/Join room .
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,686
    Setting the nickname before you join/create a room is fine. I am not aware of issues for that.

    You can check the PUN 2 version in the PhotonServerSettings inspector. It shows the relevant version number.

    Save this script as file in your project: https://hastebin.com/eceropaloy.csharp
    Make a new scene and attach it to your camera. Then build and run and also play it in the Editor. It should log all (random) nicknames.
     
    andreas3739 likes this.
  5. andreas3739

    andreas3739

    Joined:
    Jul 1, 2020
    Posts:
    14
    I'm not good at this, but I want to know if Owner.Nickname is related to the game version, I mean the version in PhotonServerSettings? I did not set the version if I'm not mistaken.

    I will make an effort later and Thank you for lending me your hand.
     
  6. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,686
    NickName is not related to the game version, no.
     
  7. andreas3739

    andreas3739

    Joined:
    Jul 1, 2020
    Posts:
    14
    I am doing this right now. So the PhotonNetwork.Nickname can be written outside the else ?
    Code (CSharp):
    1. // script in joined lobby, and attach to inputfield
    2. public InputField nameInputField;
    3.     public void setName()
    4.     {
    5.         PhotonNetwork.NickName = nameInputField.text;
    6.         Debug.Log(PhotonNetwork.NickName);
    7.     }
    8.  
    9. // second script, which is attached to the player
    10. public Text nameText;
    11.     private void Start()
    12.     {
    13.         if(photonView.IsMine){
    14.             nameText.text = PhotonNetwork.NickName;
    15.         }
    16.        
    17.     nameText.text = pv.Owner.NickName;
    18.     }
    It still did not work :(
     
  8. andreas3739

    andreas3739

    Joined:
    Jul 1, 2020
    Posts:
    14

    I just make a changes just now, I thhink this should be what you mean to sync the string. But it popped up error
    "InvalidCastException: Specified cast is not valid."

    Code (CSharp):
    1. // I try to add this code in second script
    2. public class playerName : MonoBehaviourPun, IPunObservable
    3. {
    4.     public Text nameText;
    5.     public string photonName;
    6.     public PhotonView pv;
    7.     private void Start()
    8.     {
    9.         if(photonView.IsMine){
    10.             photonName = PhotonNetwork.NickName;
    11.             nameText.text = photonName;
    12.         }
    13.      
    14.         nameText.text = pv.Owner.NickName;
    15.     }
    16.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    17.     {
    18.         if (stream.IsWriting)
    19.         {
    20.             stream.SendNext(photonName);
    21.         }
    22.         else if (stream.IsReading)
    23.         {
    24.             photonName = (string)stream.ReceiveNext();
    25.         }
    26.     }
     
    oguzhanaknc likes this.
  9. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,686
    InvalidCastException: Specified cast is not valid.
    This usually points to some line of code. What's there? Where does it cast (and from which type)?

    Not to be rude but you might want to look up how to debug those errors on your end. It will take you forever, when you are not able to understand most of these errors. Try to find tutorials on C# debugging and error code explanations.
     
unityunity