Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved NullReferenceException when Removing Item from NetworkList

Discussion in 'Netcode for GameObjects' started by Oedsdb, Sep 19, 2023.

  1. Oedsdb

    Oedsdb

    Joined:
    Nov 1, 2020
    Posts:
    4
    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:

    Code (CSharp):
    1.     private void NetworkManager_Server_OnClientDisconnectCallback(ulong clientId)
    2.     {
    3.         for (int i = 0; i < ConnectedPlayersNetworkList.Count; i++)
    4.         {
    5.             PlayerData player = ConnectedPlayersNetworkList[i];
    6.  
    7.             if (player.clientId != clientId) continue;
    8.  
    9.             ConnectedPlayersNetworkList.RemoveAt(i);
    10.             Debug.Log("Player removed from PlayerDataNetworkList.");
    11.         }
    12.     }
    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.NetworkList`1[T].MarkNetworkObjectDirty () (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/NetworkVariable/Collections/NetworkList.cs:78)
    Unity.Netcode.NetworkList`1[T].HandleAddListEvent (Unity.Netcode.NetworkListEvent`1[T] listEvent) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/NetworkVariable/Collections/NetworkList.cs:536)
    Unity.Netcode.NetworkList`1[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:DisconnectRemoteClient(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)
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    674
    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.
     
    Oedsdb likes this.
  3. Oedsdb

    Oedsdb

    Joined:
    Nov 1, 2020
    Posts:
    4
    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!
     
  4. Oedsdb

    Oedsdb

    Joined:
    Nov 1, 2020
    Posts:
    4
    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;
     
    cerestorm likes this.
  5. gps-m4jk

    gps-m4jk

    Joined:
    Dec 15, 2021
    Posts:
    3
    I had the same problem and your solution resolved it, thanks :)