Search Unity

Unet PLAYER name not same as on server and local player [HELP]

Discussion in 'UNet' started by CroHack97, Apr 5, 2017.

  1. CroHack97

    CroHack97

    Joined:
    Oct 26, 2014
    Posts:
    46
    Hey,
    downloaded Network Lobby from asset store and modified code a bit but is making me more problems.

    I need to have text same on both local player and client (I use unity editor and pc build to test)
    But when I start and connect both players to lobby names differ.

    Here are pictures of different results:
    When I start matchaking on pc first Skin gets updated and but on editor skin stays default (skullcandy player) (So it didnt change). And name changed ( "Name" is the new name and "player name" is default - didnt change)


    This is When i start match first on editor (Not on pc build like image above)
    I get totaly differnet results, (names change(but on both players) and skins stay same)



    So I get really weird results (not fammiliar a lot with unet)


    So i need to update name and UI image in lobby for player that he has chosed before.
    -NAME(of player): will be playerprefs.getstring), string will change at textfield
    - IMAGE: will be Gameobject.find("Player").getcomponent<Spriterenderer>() (thats player behind this UI)


    Here is code i Use: (its basic unet networking code, just I dont get networking, so probably need to delete line of code or something to make it all work.
    This has been bugging me a lot,
    so thanks a lot in front!

    Code (CSharp):
    1.      
    2.       public Image PlayerImage; // UI Sprite
    3.      
    4.      
    5.        //OnMyName function will be invoked on clients when server change the value of playerName
    6.         [SyncVar(hook = "OnMyName")]
    7.         public string playerName = "";
    8.         [SyncVar(hook = "OnMySkin")]
    9.         public Sprite playerSkin;
    10.        
    11.        
    12.         public override void OnClientEnterLobby()
    13.         {
    14.             base.OnClientEnterLobby();
    15.  
    16.  
    17.             if (isLocalPlayer)
    18.             {
    19.                 SetupLocalPlayer();
    20.             }
    21.             else
    22.             {
    23.                 SetupOtherPlayer();
    24.             }
    25.  
    26.             //setup the player data on UI. The value are SyncVar so the player
    27.             //will be created with the right value currently on server
    28.             OnMyName(playerName);
    29.             OnMySkin (playerSkin);
    30.         }
    31.        
    32.           public override void OnStartAuthority()
    33.         {
    34.             base.OnStartAuthority();
    35.            SetupLocalPlayer();
    36.         }
    37.        
    38.        
    39.        
    40.         void SetupOtherPlayer()
    41.         {
    42.             nameInput.interactable = false;
    43.             removePlayerButton.interactable = NetworkServer.active;
    44.  
    45.             ChangeReadyButtonColor(NotReadyColor);
    46.  
    47.             readyButton.transform.GetChild(0).GetComponent<Text>().text = "...";
    48.             readyButton.interactable = false;
    49.  
    50.             OnClientReady(false);
    51.         }
    52.  
    53.         void SetupLocalPlayer()
    54.         {
    55.  
    56.             //have to use child count of player prefab already setup as "this.slot" is not set yet
    57.             if (playerName == "") {
    58.                 //CmdNameChanged ("Player" + (LobbyPlayerList._instance.playerListContentTransform.childCount - 1));
    59.                 CmdNameChanged(PlayerPrefs.GetString ("UserName"));
    60.             }
    61.             if (playerSkin == null) {
    62.                 OnSkinChanged(GameObject.Find ("BallCollider").GetComponent<SpriteRenderer> ().sprite);
    63.             }
    64.  
    65.  
    66.             nameInput.onEndEdit.RemoveAllListeners();
    67.             nameInput.onEndEdit.AddListener(OnNameChanged);
    68.  
    69.  
    70.             //when OnClientEnterLobby is called, the loval PlayerController is not yet created, so we need to redo that here to disable
    71.             //the add button if we reach maxLocalPlayer. We pass 0, as it was already counted on OnClientEnterLobby
    72.             if (LobbyManager.s_Singleton != null) LobbyManager.s_Singleton.OnPlayersNumberModified(0);
    73.         }
    74.        
    75.        
    76.        
    77.        
    78.          public void OnMyName(string newName)
    79.         {
    80.             playerName = newName;
    81.             nameInput.text = playerName;
    82.         }
    83.  
    84.         public void OnMySkin(Sprite newSkin)
    85.         {
    86.             playerSkin = newSkin;
    87.             PlayerImage.overrideSprite = newSkin;
    88.         }
    89.        
    90.        
    91.                 [Command]
    92.         public void CmdSkinChanged(Sprite Skin)
    93.         {
    94.             playerSkin = Skin;
    95.         }
    96.  
    97.         [Command]
    98.         public void CmdNameChanged(string name)
    99.         {
    100.             playerName = name;
    101.         }