I was following a tutorial from code monkey where he makes a network list and seralizes it as shown in the pictures. But when i do this i get a Object refrence not set to an instance of an object. I'm not sure if im initializing the list or serializing it wrong so any help would be appreciated 🙂
#Not Serializing Network List right
1 messages · Page 1 of 1 (latest)
I wouldn't use a singleton with networking. They tend to get overwritten when objects spawn.
I would use NetworkVariable<Dictionary<int,PlayerData>>() instead of network list
NullReferenceException: Object reference not set to an instance of an object
GameMultiplayer.NetworkManager_OnClientConnectedCallback (System.UInt64 clientId) (at Assets/Scripts/UI/MainMenu/GameMultiplayer.cs:88)
private void NetworkManager_OnClientConnectedCallback(ulong clientId)
{
playerDataNetworkDic.Value.TryAdd(clientId, new PlayerData
{
clientId = clientId,
modelID = 0,//GetFirstUnusedColorId(),
});
Im still confused on the null refrence my playerDataNetworkDic. do i need to initialize it once the game activates?
Yes the dictionary need to be initialized along with the network variable
Where should I initialize this? I currently just have it as
NetworkVariable<Dictionary<int,PlayerData>>PlayerDataDic = NetworkVariable<Dictionary<int,PlayerData>>() do I need to initialize this on network spawn?
You can initialize it in line. NetworkVariable<Dictionary<int,PlayerData>>PlayerDataDic = new NetworkVariable<Dictionary<int,PlayerData>>(new Dictionary<int,PlayerData>);
hey sorry 😅
NullReferenceException: Object reference not set to an instance of an object
GameMultiplayer.NetworkManager_OnClientConnectedCallback (System.UInt64 clientId) (at Assets/Scripts/UI/MainMenu/GameMultiplayer.cs:88)
I called it like this
private NetworkVariable<Dictionary<ulong, PlayerData>> playerDataNetworkDic = new NetworkVariable<Dictionary<ulong, PlayerData>>(new Dictionary<ulong, PlayerData>());
this was super easy for me to setup in the past so im super confused. On this null ref might have to give it up in a second to stop bothering you lol.
That error doesn't sound like it has anything to do with the network variable
yeah it doesnt after some testing i realized im a dummy i think its running to fast when the game first starts is their a good way i should be delaying or calling this?
oh are you trying to set the network variable in the client connection callback?
yes NetworkManager.Singleton.OnClientConnectedCallback += NetworkManager_OnClientConnectedCallback;
and than i want to add the player data when they join the game
Ok yea, that is too soon. the client isn't actually joined at that point. use OnNetworkSpawn() instead