Null reference exception when closing server

After doing exactly as lecture had shown, I get this constant error everytime I close server

It leads to this place in code

.

I think we resolve this error at the end of the section. If you complete the section and the error persists, let me know and I can help you troubleshoot it.

Hi @Yitzchak_Cohen thanks for responding on my issue

I’ve finished the “Gameplay Additions” section till the end and implemented everything the lecturer had shown me with no buggs except this issue, which still persists

Can you help me? Do you need me to send you my github project?

BTW this issue happens only when I close the editor, whilst hosting a game
If I leave the hosted game to main menu (using Leave Game button) and only then close the editor, then there’s no error.

Oh this might be a bug I am seeing other users get. What version of Unity and Netcode for GameObjects are you using?

It could be that the network list is being cleaned up automatically when you close the game in newer versions. So when you try and clean it up manually it’s already gone. If you just comment out the foreach loop, does everything work correctly?

My Unity editor version is 2021.3.9f1
The NGO package version is 1.6.0

Yes, commenting out the foreach loop made so that now there’s no errors pop up in console.
But this code is necessary to clear up leader board items whenever some client leaves the game.

Hmm, that is true, perhaps we need to put the block of code in an IsClientOnly check instead of commenting it out completely.

Okay, my new solution is to add this check before the foreach loop:

if(IsServer && player.OwnerClientId == OwnerClientId)
{
      return;
}

That should let the clients clean up but not the server.

Thank you, it helped fix my issue

I also got other error messages, but they were caused only when I close the editor,
If I make a proper game exit like: pressing Leave Game and only then try to close - then there are no errors.

1 Like

What other error messages did you receive?

Here are the steps when I get error:

  1. Build and Run

  2. On editor launch and create Host

  3. On build join the Host’s lobby

  4. On editor I just close the game
    image

  5. Then I get this error:

which lead to this code again:

If there are more than one client joined, let’s say 2 clients joined, then there are 2 errors:
I also sometimes get this lobby error:

Note: The error does not occur if host is alone and no clients are joined to the game, which was my initial issue, that was fixed by your suggestion.

I thinks it’s the deal of order of closing stuff, when editor is closing game, then each system (lobby, host, client, relay etc.) has to close in some orderly manner.
If it’s really a lack in my project (which is just copy of lecturer’s), then it must be foresought and handled in some code.
I might be wrong though.

Try moving the new check outside of the foreach loop. That’s how I have it and I am not getting any errors.

I moved this code just before foreach loop
It did not help though, I get same errors in same use cases

    private void HandlePlayerDespawn(PlayerInstance player)
    {
        if (leaderBoardEntityStates == null)
        {
            return;
        }
        if (IsServer && player.OwnerClientId == OwnerClientId)
        {
            return;
        }
        foreach (LeaderBoardEntityState item in leaderBoardEntityStates)
        {
            if (item.ClientId != player.OwnerClientId)
            {
                continue;
            }
            if (leaderBoardEntityStates.Contains(item))
            {
                leaderBoardEntityStates.Remove(item);
            }
            break;
        }
        player.Inventory.TotalCoins.OnValueChanged -= (oldCoins, newCoins) => HandleCoinsChange(player.OwnerClientId, newCoins);
    }

PlayerInstance is same as TankPlayer class

Weird that code should just not be running when the server leaves the game, that was the goal of the check.

Can you check if it’s running HandlePlayerDespawn for other clients when it’s the host leaving the game?

Or maybe it doesn’t matter, maybe we should remove the IsServer part of the check. We can just not run this code when we are the ones leaving the game. Would you like to try that? Just use the OwnerClientId check?

Tried and thoroughly tested, still the same errors

    private void HandlePlayerDespawn(PlayerInstance player)
    {
        if (leaderBoardEntityStates == null)
        {
            return;
        }
        if (player.OwnerClientId == OwnerClientId)
        {
            return;
        }
        foreach (LeaderBoardEntityState item in leaderBoardEntityStates)
        {
            if (item.ClientId != player.OwnerClientId)
            {
                continue;
            }
            if (leaderBoardEntityStates.Contains(item))
            {
                leaderBoardEntityStates.Remove(item);
            }
            break;
        }
        player.Inventory.TotalCoins.OnValueChanged -= (oldCoins, newCoins) => HandleCoinsChange(player.OwnerClientId, newCoins);
    }

Ah well, would you like to upload your project here:
https://gdev.tv/projectupload
I can take a look at it and try and do some local debugging.

Done, hope it helps figure out issue

Well I couldn’t get the build to link to Unity Services (worked fine in the editor). Really not sure what is going on there. I’m going to try updating to Unity 2022 and see if that fixes it.

Privacy & Terms