Search Unity

Resolved I need an help with network variable (string)

Discussion in 'Multiplayer' started by TheCreatorofSouth, Feb 12, 2022.

  1. TheCreatorofSouth

    TheCreatorofSouth

    Joined:
    Aug 28, 2021
    Posts:
    1
    I'm making my first multiplayer game. I want the player name to change and read on the screen, but
    unity tells me that it is not possible to implicitly convert type 'string' to 'Unity.Netcode.NetworkVariable <string>'. Please I need some help.
    This is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using Unity.Netcode;
    4. using Unity.Netcode.Components;
    5. using Unity.Netcode.Transports;
    6. using System;
    7.  
    8. public class SetupLocalPlayer : NetworkBehaviour {
    9.  
    10.  
    11.     public NetworkVariable<string> pname;
    12.     public NetworkObject cameraplace;
    13.  
    14.     void OnGUI()
    15.     {
    16.         if (IsLocalPlayer)
    17.         {
    18.             Camera.main.transform.position = cameraplace.transform.position;
    19.             Camera.main.transform.rotation = cameraplace.transform.rotation;
    20.             pname = GUI.TextField(new Rect(25, Screen.height - 40, 100, 30), pname);
    21.             if (GUI.Button(new Rect(130, Screen.height - 40, 100, 30), "Change Name"))
    22.             {
    23.                 CmdChangeName(pname);
    24.             }
    25.         }
    26.     }
    27.    
    28.     [ServerRpc]
    29.     public void CmdChangeName(string newName)
    30.     {
    31.         pname = newName;
    32.     }
    33.  
    34.     void Start () {
    35.         if (IsLocalPlayer)
    36.             GetComponent<drive>().enabled = true;
    37.     }
    38.     void Update()
    39.     {
    40.         this.GetComponentInChildren<TextMesh>().text = pname.Value;
    41.     }
    42. }
    43.  
    44.  
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    660
    You can no longer have a NetworkVariable of type string (although the documentation may still suggest otherwise). You can use a type such as FixedString64Bytes instead (NetworkVariable<FixedString64Bytes>).
     
    gooby429 and RikuTheFuffs like this.
  3. tyler_251

    tyler_251

    Joined:
    Mar 6, 2022
    Posts:
    1
    Be sure to import "using Unity.Collections" to use FixedString64Bytes
     
  4. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Hi @TheCreatorofSouth , as cerestorm mentioned you need to use a FixedString, like this:

    Code (CSharp):
    1. using Unity.Collections;
    2.  
    3. public class SetupLocalPlayer : NetworkBehaviour
    4. {
    5.      NetworkVariable<FixedString128Bytes> pname = new NetworkVariable<FixedString128Bytes>();
    6. }
    The amount of bytes in the string depends on how big the string is supposed to be. 1 ASCII character = 1 byte and 1 UNICODE character = 2/3/4 bytes, so you can plan accordingly to the maximum length of your player's name. (be mindful of oriental languages as well!)
     
  5. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    660
    You can use strings directly if you let Netcode know how to serialize it with UserNetworkVariableSerialization as mentioned here. There doesn't seem to be any documentation explaining how to use it which would be useful.