Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Reading variables values assigned to prefabs spawned on network

Discussion in 'Netcode for GameObjects' started by marzdh, Aug 20, 2023.

  1. marzdh

    marzdh

    Joined:
    Jun 3, 2013
    Posts:
    35
    Every player that spawns on the network gets a name assigned to them. How do I ensure that the name assigned to each player to visible to all players?

    If I am using the code below, I can only see my name, but not the name assigned to other players who join in at different times during the game.

    Code (CSharp):
    1. public void SetPlayerData()
    2.     {
    3.         if (IsOwner)
    4.         {
    5.             playerName.text = Bootstrap.instance.playerName;
    6.             Debug.Log(Bootstrap.instance.playerName);
    7.         }
    8.     }
    I'm new to NGO so the question so my question may sound very basic.
     
  2. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    188
  3. marzdh

    marzdh

    Joined:
    Jun 3, 2013
    Posts:
    35
    Doesn't seem to be working with Network Variables. The name set for the host is also set for the clients. Also, only the hostcansee the name, the clients can't. Maybe I am doing it incorrectly.

    Code (CSharp):
    1. private NetworkVariable<FixedString128Bytes> m_SomeValue = new("", NetworkVariableReadPermission.Everyone);
    2.  
    3.     public override void OnNetworkSpawn() {
    4.         base.OnNetworkSpawn();
    5.  
    6.         SetPlayerData();
    7.         if (!IsServer) m_SomeValue.OnValueChanged += NameChanged;
    8.     }
    9.  
    10.     //Changes the name on the server
    11.     public void SetPlayerData()
    12.     {
    13.         m_SomeValue.Value = Bootstrap.instance.playerName;
    14.         playerName.text = m_SomeValue.Value.toString();
    15.     }
    16.  
    17. //Changes the name for each client and display to everyone
    18.     private void NameChanged(FixedString128Bytes previousValue, FixedString128Bytes newValue)
    19.     {
    20.         playerName.text = newValue.toString();
    21.     }