- Home /
Can't initialize NetworkVariables with NetCode for GameObjects Unity 2020.3.31f1
Hey everyone! I recently started migrating some old UNet Multiplayer project to NetCode. All the RPC stuff is working fine, but I got some trouble with NetworkVariables.
using UnityEngine;
using Unity.Netcode;
public class NameManager : NetworkBehaviour
{
NetworkVariable<string> playerName = new NetworkVariable<string>();
NetworkVariable<float> playerHealth = new NetworkVariable<float>();
}
The compiler tells met, that the type 'String' must be a non-nullable value to be used as a parameter T (CS0453) and the type 'float' also can't be used as T, since there is no boxing conversion from 'float' to '?' (CS0315).
I wasn't abled to find the my error, nor does it seem like anybody else has it, so here we go! Does anybody know, why this error is occuring and how to fix it?
I know, I shouldn't post two errors at the same time, but if they are connected, this might be useful. If you need any more informations, feel free to ask.
Answer by wallstop · Apr 14 at 10:42 PM
string is a managed type, NetworkVariables require unmanaged types (structs, primitives, enums).
To deal with strings, you need to use one of Unity Collection's FixedStringBytes, such as FixedString64Bytes.
Your answer
Follow this Question
Related Questions
Shop system using Enum throws up errors. 0 Answers
How to sync a varible managed by the clients using UNET? 0 Answers
x=x Assignment made to same variable 3 Answers
Weird error every frame 1 Answer
C# - variable is assigned but its value is never used 3 Answers