Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Setting player name in Menu, then online

Discussion in 'Multiplayer' started by mrgohut, Jan 13, 2016.

  1. mrgohut

    mrgohut

    Joined:
    Feb 8, 2014
    Posts:
    134
    Hi :)
    In Menu scene i've got input field where player type his nick, then he join the game:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using UnityEngine.Networking;
    5.  
    6. public class Network_Manager : NetworkManager {
    7.  
    8.     void Awake()
    9.     {
    10.         playerName = GameObject.Find("CharacterName").transform.FindChild("Text").GetComponent<Text>();
    11.     }
    12.  
    13.     public void JoinGame()
    14.     {
    15.         if (CharacterName() != null)
    16.         {
    17.             PlayerPrefs.SetString("PlayerName", CharacterName());
    18.             SetPort();
    19.             SetIPAdress();
    20.             StartClient();
    21.         }
    22.         else
    23.         {
    24.             Debug.LogWarning("You need to set your character name!");
    25.         }
    26.     }
    27. }
    When he joined the game:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4.  
    5. public class PlayerManager : NetworkBehaviour {
    6.  
    7.     public string playerName;
    8.  
    9.     [SerializeField]
    10.     private Transform myTransform;
    11.     public Transform myCam;
    12.  
    13.     void Awake()
    14.     {
    15.         myTransform = transform;
    16.     }
    17.  
    18.     public override void OnStartLocalPlayer()
    19.     {
    20.         GetComponent<PlayerMotor>().enabled = true;
    21.         GetComponent<PlayerController>().enabled = true;
    22.         CmdSetNick(PlayerPrefs.GetString("PlayerName"));
    23.     }
    24.  
    25.     [Command]
    26.     void CmdSetNick(string nickname)
    27.     {
    28.         playerName = nickname;
    29.         myTransform.name = playerName;
    30.         RpcUpdateNick(nickname);
    31.     }
    32.  
    33.     [ClientRpc]
    34.     void RpcUpdateNick(string nick)
    35.     {
    36.         playerName = nick;
    37.         myTransform.name = playerName;
    38.     }
    39. }
    40.  
    But now is the problem...
    When i want to check for object name and in-script player name:
    Code (csharp):
    1.  
    2. RaycastHit hit;
    3.         if(Physics.Raycast(myCam.TransformPoint(0, 0, 0.5f), myCam.forward, out hit, 5))
    4.         {
    5.             if(hit.transform.tag == "Player")
    6.                 Debug.LogError("Object name: " + hit.transform.name + "\n Object in-script name: " + hit.transform.GetComponent<PlayerManager>().playerName);
    7.         }
    it only works on host client (1st player), but when i'm using it on second player:
    What's wrong with this script? I'm still having a hard time to get my head around UNET (command, clientRPC, client, etc...)
     
    Last edited: Jan 13, 2016
  2. SuperNeon

    SuperNeon

    Joined:
    Mar 16, 2014
    Posts:
    85
    Hi !

    You can try to add [SyncVar] attribute on playerName as a quick fix.
     
  3. mrgohut

    mrgohut

    Joined:
    Feb 8, 2014
    Posts:
    134
    Thanks. It's working now, but only playerName var. How can i sync Transform name too?
    Code (csharp):
    1.  
    2. [Client]
    3. void Set()
    4. {
    5. myTransform.name = playerName;
    6. }
    7.  
    ??
     
  4. mrgohut

    mrgohut

    Joined:
    Feb 8, 2014
    Posts:
    134
    Okay, it's me again :D
    When i remove ClientRPC from my script everything is working fine aswell. I really don't understand now :/