Hello, I am working on adding a scoreboard to my game, so I made a struct that holds player info, made it serializable, and want to add them to a list that syncs across the network.
When someone joins the server, they are added to that list, however im getting a lengthy NullReferenceException that doesn’t quite give me enough information.
I’ve tried looking through the error many times and tracking down where the real problem is, but I cant seem to find it. Can anyone help?
I believe these are the relevant code blocks:
This is the class which is meant to store and manage the list of players:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
using System;
public unsafe class ActiveNetwork : NetworkBehaviour
{
public NetworkList<NetPlayer> players;
public int size;
public struct NetPlayer : INetworkSerializable,IEquatable<NetPlayer>
{
public ulong id;
public int kills;
public int damage;
public int deaths;
public fixed char username[32];
public NetPlayer(ulong id, string username, int kills, int damage, int deaths)
{
this.id = id;
this.kills = kills;
this.damage = damage;
this.deaths = deaths;
SetUsername(username);
}
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
serializer.SerializeValue(ref id);
serializer.SerializeValue(ref kills);
serializer.SerializeValue(ref damage);
serializer.SerializeValue(ref deaths);
for (int i = 0; i < 32; ++i)
{
serializer.SerializeValue(ref username[i]);
}
}
public void SetUsername(string user)
{
for (int i = 0; i < Math.Min(user.Length, 32); ++i)
{
username[i] = user[i];
}
}
public string GetUsername()
{
fixed (char*p = username) { return new string(new ReadOnlySpan<char>(p, 32)); }
}
public bool Equals(NetPlayer other)
{
return false;
}
}
void Awake()
{
players = new NetworkList<NetPlayer>();
}
public void Populate()
{
Debug.Log("Populated Player List");
foreach (GameObject player in FindObjectsOfType<GameObject>())
{
if (player.GetComponent<Player>() != null)
{
Player activePlayer = player.GetComponent<Player>();
players.Add(new NetPlayer(activePlayer.OwnerClientId, activePlayer.username, 0, 0, 0));
Debug.Log("Found " + activePlayer.username + " On The Network!");
size++;
}
}
}
public void Refresh()
{
players.Clear();
size = 0;
Populate();
}
public void AddPlayer(NetPlayer player)
{
Debug.Log("Added " + player.GetUsername() + " To The Network!");
players.Add(player);
size++;
}
}
This is where the player attempts to join the server and have itself added to the list:
private ActiveNetwork network;
private Scoreboard scoreboard;
public override void OnNetworkSpawn()
{
network = (ActiveNetwork)FindObjectOfType(typeof(ActiveNetwork));
scoreboard = (Scoreboard)FindObjectOfType(typeof(Scoreboard));
network.players.OnListChanged += TestOnOnListChanged;
if (IsOwner)
{
username = Profile.username;
Debug.Log("Id Is: " + OwnerClientId + " Username Is: " + username);
SendUserServerRpc(OwnerClientId, username);
}
}
[ServerRpc]
void SendUserServerRpc(ulong id, string user)
{
Debug.Log("Client: " + user + " Joined! ");
this.username = user;
Debug.Log(network.players == null ? "No Network List Found" : "Network List Found");
network.AddPlayer(new ActiveNetwork.NetPlayer(id, user, 0, 0, 0));
}
And Here is the error in question:
NullReferenceException: Object reference not set to an instance of an object
Unity.Netcode.NetworkList`1[T].MarkNetworkObjectDirty () (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/NetworkVariable/Collections/NetworkList.cs:68)
Unity.Netcode.NetworkList`1[T].HandleAddListEvent (Unity.Netcode.NetworkListEvent`1[T] listEvent) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/NetworkVariable/Collections/NetworkList.cs:499)
Unity.Netcode.NetworkList`1[T].Add (T item) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/NetworkVariable/Collections/NetworkList.cs:386)
ActiveNetwork.AddPlayer (ActiveNetwork+NetPlayer player) (at Assets/Networking/ActiveNetwork.cs:118)
Player.SendUserServerRpc (System.UInt64 id, System.String user) (at Assets/Player/Player.cs:54)
Player.__rpc_handler_4236175673 (Unity.Netcode.NetworkBehaviour target, Unity.Netcode.FastBufferReader reader, Unity.Netcode.__RpcParams rpcParams) (at <38f293a5a37544e3beccfc416df1918a>:0)
Unity.Netcode.RpcMessageHelpers.Handle (Unity.Netcode.NetworkContext& context, Unity.Netcode.RpcMetadata& metadata, Unity.Netcode.FastBufferReader& payload, Unity.Netcode.__RpcParams& rpcParams) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/Messaging/Messages/RpcMessages.cs:77)
Rethrow as Exception: Unhandled RPC exception!
UnityEngine.Debug:LogException(Exception)
Unity.Netcode.RpcMessageHelpers:Handle(NetworkContext&, RpcMetadata&, FastBufferReader&, __RpcParams&) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/Messaging/Messages/RpcMessages.cs:81)
Unity.Netcode.ServerRpcMessage:Handle(NetworkContext&) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/Messaging/Messages/RpcMessages.cs:122)
Unity.Netcode.NetworkBehaviour:__endSendServerRpc(FastBufferWriter&, UInt32, ServerRpcParams, RpcDelivery) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/Core/NetworkBehaviour.cs:93)
Player:SendUserServerRpc(UInt64, String) (at Assets/Player/Player.cs:48)
Player:OnNetworkSpawn() (at Assets/Player/Player.cs:30)
Unity.Netcode.NetworkBehaviour:InternalOnNetworkSpawn() (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/Core/NetworkBehaviour.cs:439)
Unity.Netcode.NetworkObject:InvokeBehaviourNetworkSpawn() (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/Core/NetworkObject.cs:817)
Unity.Netcode.NetworkSpawnManager:SpawnNetworkObjectLocallyCommon(NetworkObject, UInt64, Boolean, Boolean, UInt64, Boolean) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/Spawning/NetworkSpawnManager.cs:552)
Unity.Netcode.NetworkSpawnManager:SpawnNetworkObjectLocally(NetworkObject, UInt64, Boolean, Boolean, UInt64, Boolean) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/Spawning/NetworkSpawnManager.cs:453)
Unity.Netcode.NetworkManager:HandleConnectionApproval(UInt64, ConnectionApprovalResponse) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/Core/NetworkManager.cs:2044)
Unity.Netcode.NetworkManager:StartHost() (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/Core/NetworkManager.cs:1140)
NetworkManagerUI:<Awake>b__3_0() (at Assets/Networking/NetworkManagerUI.cs:17)
UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:517)