NullReferenceException when Removing Item from NetworkList

Description:
I’m encountering a critical issue in my Unity project related to a Netcode for Gameobjects’s NetworkLists, particularly when the Host leaves the game, which triggers a disconnect callback which triggers for all clients, where the players are removed from a NetworkList of connected players.

Problem:
In my code, I have a method called NetworkManager_Server_OnClientDisconnectCallback where I attempt to remove a player from a NetworkList called PlayerDataNetworkList. This is also called when the host itself leaves. This causes a NullReferenceException. This is the code:

    private void NetworkManager_Server_OnClientDisconnectCallback(ulong clientId)
    {
        for (int i = 0; i < ConnectedPlayersNetworkList.Count; i++)
        {
            PlayerData player = ConnectedPlayersNetworkList[i];

            if (player.clientId != clientId) continue;

            ConnectedPlayersNetworkList.RemoveAt(i);
            Debug.Log("Player removed from PlayerDataNetworkList.");
        }
    }

This function is only called on the server / host. Although I understand that probably all clients should disconnect anyway when the host leaves, I still would like to understand the cause of this particular error: the host is found ‘in’ this NetworkList, and the function tries to remove it (after it confirmed it is in there as it would otherwise continue to the next iteration). So what am I overlooking here?

I would greatly appreciate help!

Please tell me if I need to provide more information for you to try to analyze the problem. I’ve only posted once before.

The full error:
NullReferenceException: Object reference not set to an instance of an object
Unity.Netcode.NetworkList1[T].MarkNetworkObjectDirty () (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/NetworkVariable/Collections/NetworkList.cs:78) Unity.Netcode.NetworkList1[T].HandleAddListEvent (Unity.Netcode.NetworkListEvent1[T] listEvent) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/NetworkVariable/Collections/NetworkList.cs:536) Unity.Netcode.NetworkList1[T].RemoveAt (System.Int32 index) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/NetworkVariable/Collections/NetworkList.cs:503)
MultiplayerManager.NetworkManager_Server_OnClientDisconnectCallback (System.UInt64 clientId) (at Assets/Scripts/LobbyScripts/MultiplayerManager.cs:75)
Unity.Netcode.NetworkConnectionManager.OnClientDisconnectFromServer (System.UInt64 clientId) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/Connection/NetworkConnectionManager.cs:850)
UnityEngine.Debug:LogException(Exception)
Unity.Netcode.NetworkConnectionManager:OnClientDisconnectFromServer(UInt64) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/Connection/NetworkConnectionManager.cs:854)
Unity.Netcode.NetworkConnectionManager:smile:isconnectRemoteClient(UInt64) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/Connection/NetworkConnectionManager.cs:877)
Unity.Netcode.NetworkConnectionManager:Shutdown() (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/Connection/NetworkConnectionManager.cs:976)
Unity.Netcode.NetworkManager:ShutdownInternal() (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/Core/NetworkManager.cs:980)
Unity.Netcode.NetworkManager:OnDestroy() (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/Core/NetworkManager.cs:1043)
Unity.Netcode.NetworkManager:OnApplicationQuit() (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/Core/NetworkManager.cs:1037)

Sounds similar to this issue. It may be too late to modify a network value at that point but maybe not, we’ll have to see what the Unity devs have to say.

1 Like

I read the link, and I think you might be onto something yes! Although it’s not sure, I’ll try some workarounds checking if the application is quitting or something similar to see if that resolves it. Thank you @cerestorm !

I believe you were correct @cerestorm . all I can say for sure, is that cancelling the function in case the server was shutting down resolved the error.

if (NetworkManager.ShutdownInProgress) return;

1 Like

I had the same problem and your solution resolved it, thanks :slight_smile: