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 ’. Please I need some help.
This is my code:
using UnityEngine;
using System.Collections;
using Unity.Netcode;
using Unity.Netcode.Components;
using Unity.Netcode.Transports;
using System;
public class SetupLocalPlayer : NetworkBehaviour {
public NetworkVariable<string> pname;
public NetworkObject cameraplace;
void OnGUI()
{
if (IsLocalPlayer)
{
Camera.main.transform.position = cameraplace.transform.position;
Camera.main.transform.rotation = cameraplace.transform.rotation;
pname = GUI.TextField(new Rect(25, Screen.height - 40, 100, 30), pname);
if (GUI.Button(new Rect(130, Screen.height - 40, 100, 30), "Change Name"))
{
CmdChangeName(pname);
}
}
}
[ServerRpc]
public void CmdChangeName(string newName)
{
pname = newName;
}
void Start () {
if (IsLocalPlayer)
GetComponent<drive>().enabled = true;
}
void Update()
{
this.GetComponentInChildren<TextMesh>().text = pname.Value;
}
}