Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Multiplayer "String" above playermodel

Discussion in 'Multiplayer' started by JonasrDJ, Jul 30, 2014.

  1. JonasrDJ

    JonasrDJ

    Joined:
    Jul 15, 2012
    Posts:
    8
    Hi guys. I've made a very simple script that is supposed to work as a playername. Its a 3D text above the playermodel & it all works. The player inputs his name in a textfield before connecting (updating the string) & The input will be show as his name when spawning. My problem is, that the string they input, update the before connected players rather than their own, while the person connecting is getting the previous connected players name. I'm aware of "If(networkView.isMine), but since they update the string before connecting, in the textfield I can't apply this.

    The script looks like following.

    Testname.js - Linked to the 3D text.

    Code (JavaScript):
    1. GetComponent(TextMesh).text = General.Username;    
    General.js - Linked to my MainMenu (Join Game button)

    Code (JavaScript):
    1. static var Username : String = "Username";  
    2.  
    3.  
    4. function OnGUI() {
    5. Username = GUI.TextField (Rect (10, 10, 200, 20), Username, 25);
    6. }
    7.  
    8.  
    9.  
    I appreciate any help. Thank you.
     
  2. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Why can't you use networkView.isMine ?
    Doesn't matter when they input the name.
    Upon sending the data it will check.
     
  3. JonasrDJ

    JonasrDJ

    Joined:
    Jul 15, 2012
    Posts:
    8
    Because there is no networkView when you haven't connected to anything (As far as I know, correct me if I'm wrong.) And therefore the GUI.Textfield won't show up.

    Code (JavaScript):
    1. static var Username : String = "Username";  
    2.  
    3. function OnGUI() {
    4. if(networkView.isMine) {
    5. Username = GUI.TextField (Rect (10, 10, 200, 20), Username, 25);
    6. }
    7. }
    I could also add it here instead

    Code (JavaScript):
    1. if(networkView.isMine) {
    2. GetComponent(TextMesh).text = General.Username;  
    3. }
    But if I do it like this, the text does not return the string and I can't figure out why.
     
  4. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    The networkView exists if you have attached it to the GameObject.
    Even when not connected, it should work.
     
  5. JonasrDJ

    JonasrDJ

    Joined:
    Jul 15, 2012
    Posts:
    8
    I have attached the Network View to the gameobject with the script, but the
    No matter what I do,
    Code (JavaScript):
    1.     if(networkView.isMine) {
    2.     GetComponent(TextMesh).text = General.Username;
    3.     }
    4.  
    Won't work if I have "if(networkView.isMine)" added.
     
  6. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Hmmm, now i'm not sure.
    Any existing GameObject in the scene prior to a connection, belong to the server once connected.
    So i'm not sure in this case what happens but you can easily test that.
     
  7. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Save the name as a PlayerPref before connecting, then when connected get it back and send it to others by an RPC as all buffered.
     
    Last edited: Aug 3, 2014
  8. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    Set PhotonNetwork.playerName before you connect and you can access it for each PhotonPlayer.

    The player who created a GameObject is stored in PhotonView.owner of the GameObject (check if the GO has a PhotonView).

    This is what I do:
    http://hastebin.com/givijomoto.cs
     
  9. JonasrDJ

    JonasrDJ

    Joined:
    Jul 15, 2012
    Posts:
    8
    This was what I was looking for. Thank you!