question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[NetworkAnimator bug] Cannot re-host a game after pressing 'return to menu' from the BossRoom scene - NetworkAnimator null reference exception (does not happen after going to main menu from CharSelect or PostGame)

See original GitHub issue

Describe the bug Using the ‘return to menu’ button in the BossRoom scene causes a NullReferenceException to be thrown by NetworkAnimator.OnClientConnectedCallback upon attempting to host a new game. NetworkManager.StartHost() itself works, but as this exception prevents the game from going to CharSelect, the player, with no way of stopping the NetworkManager, is stuck and is forced to quit the game.

To Reproduce

  1. Enter play mode
  2. Host via IP, click on ‘host’
  3. Pick a character on CharSelect, wait for the main Boss Room scene to load
  4. Use the ‘go to main menu’ button within the Boss Room scene
  5. Attempt to host a new game via IP, click the ‘host’ button again.
  6. Error (NullReferenceException within Unity.Netcode.Components.NetworkAnimator.OnClientConnectedCallback).
    • The NetworkManager.StartHost() call succeeds, but the exception prevents CharSelect from being loaded.
    • PopupManager does not show a ‘hosting failed’ error dialog in the game itself when this happens. Clicking the ‘host’ button again will cause the ‘hosting failed’ error dialog to appear (Cannot start Host while an instance is already running message appears in console).

Expected behavior The game should proceed to the ‘CharSelect’ scene, allowing the user to continue hosting the game as expected.

Screenshots NullReferenceException when attempting to rehost The above screenshot shows what happens after clicking on ‘Host’ after first clicking ‘return to menu’ as a host in the BossRoom scene. As you can see, the NetworkManager has been able to start hosting, but due to a null reference exception in NetworkAnimator, the game itself hasn’t been able to proceed to the Charselect scene from the MainMenu.

Environment (please complete the following information):

  • OS: Windows 8.1
  • Unity Version: 2021.3.3f1
  • BossRoom Version: v1.3.0-pre

Additional context

  • This problem only happens if you press ‘return to menu’ from the BossRoom scene. The ‘return to main menu’ buttons in CharSelect and PostGame do not have this problem.
  • Haven’t tested if the problem affects relay connections, but chances are it will affect those as well.
  • Haven’t tested if it affects clients yet (whether voluntarily disconnecting or involuntarily disconnecting).

Stack trace

NullReferenceException: Object reference not set to an instance of an object
Unity.Netcode.Components.NetworkAnimator.OnClientConnectedCallback (System.UInt64 playerId) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.10/Components/NetworkAnimator.cs:446)
Unity.Netcode.NetworkManager.InvokeOnClientConnectedCallback (System.UInt64 clientId) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.10/Runtime/Core/NetworkManager.cs:352)
Unity.Netcode.NetworkManager.HandleConnectionApproval (System.UInt64 ownerClientId, Unity.Netcode.NetworkManager+ConnectionApprovalResponse response) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.10/Runtime/Core/NetworkManager.cs:2028)
Unity.Netcode.NetworkManager.StartHost () (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.10/Runtime/Core/NetworkManager.cs:1080)
Unity.Multiplayer.Samples.BossRoom.GameNetPortal.StartHost () (at Assets/Scripts/Gameplay/ConnectionManagement/GameNetPortal.cs:217)
Unity.Multiplayer.Samples.BossRoom.GameNetPortal.StartHost (System.String ipaddress, System.Int32 port) (at Assets/Scripts/Gameplay/ConnectionManagement/GameNetPortal.cs:185)
Unity.Multiplayer.Samples.BossRoom.Visual.IPUIMediator.HostIPRequest (System.String ip, System.String port) (at Assets/Scripts/Gameplay/UI/IPUIMediator.cs:83)
Unity.Multiplayer.Samples.BossRoom.Visual.IPHostingUI.OnCreateClick () (at Assets/Scripts/Gameplay/UI/IPHostingUI.cs:43)
UnityEngine.Events.InvokableCall.Invoke () (at <f1212ad1dec44ce7b7147976b91869c3>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <f1212ad1dec44ce7b7147976b91869c3>:0)
UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:70)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:114)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:57)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
NoelStephensUnitycommented, Jun 29, 2022

@TheCaveOfWonders I am truly sorry you are experiencing this bug as it is a regression bug from some of my recent updates/fixes applied to NetworkAnimator. Currently we don’t have an ETA for when the next update will be made available since we just published the v1.0.0 release version today. So, for the time being there are two paths you can take to work around this issue:

Download the V1.0.0 source code

You can always download the v1.0.0 source code source code and remove the IsServer check within the NetworkAnimator.Cleanup method so the callback is always removed. The modified method would then look like this:

private void Cleanup()
{
    if (m_NetworkAnimatorStateChangeHandler != null)
    {
        m_NetworkAnimatorStateChangeHandler.DeregisterUpdate();
        m_NetworkAnimatorStateChangeHandler = null;
    }
    
    //Note: This is where the IsServer check is removed
    NetworkManager.OnClientConnectedCallback -= OnClientConnectedCallback;

    if (m_CachedAnimatorParameters != null && m_CachedAnimatorParameters.IsCreated)
    {
        m_CachedAnimatorParameters.Dispose();
    }
    if (m_ParameterWriter.IsInitialized)
    {
        m_ParameterWriter.Dispose();
    }
}

The only other adjustment you would need to make, to apply those changes to your project, is to open the package manager, select a package from disk, and select the <path where you extracted the source code>\com.unity.netcode.gameobjects-1.0.0\com.unity.netcode.gameobjects\package.json file. In the next update, this will most definitely be fixed and then you can update to that new package.

Continue Using the Package

Currently, I have found several temporary work arounds when using the Netcode for GameObjects package. However, the only one that didn’t exceed the complexity of the previous option was to destroy the NetworkManager instance once it is completely shutdown (sub-optimal). Then you would need to create a new instance if you wanted to start another session.
Note: You will want to save the NetworkManager.NetworkConfig of the NetworkManager instance you are destroying and apply that to the next instance you create.

Let me know if you have any further questions or issues with this particular bug and if either temporary work around helped you resolve your issue (until the fix is published).

1reaction
LPLafontaineBcommented, Jun 27, 2022

Hi @11BelowStudio , Thank you for submitting this issue! After some investigation, it seems to be an issue with the NetworkAnimator itself. It looks like it does not properly unregister the OnClientConnected callback when the server shuts down. I’ll transfer this issue to the Netcode for GameObjects project.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Network Animator throws NullReferenceException and ...
Resolved Network Animator throws NullReferenceException and doesn't let the game build.
Read more >
Unity Network Animator null reference exception : r/Unity3D
A simple call to play an animation (which DOES exist and works in single player mode) throws a null reference exception error at...
Read more >
Null reference exception - General Support
I received the Null Reference Exception error that appears to affect modders and multi-player users mostly. I'm neither of the above.
Read more >
What is a Null Reference Exception?
A NullReferenceException happens when you try to access a reference variable that isn't referencing any object. If a reference variable isn't referencing an ......
Read more >
CHANGELOG.md
A small-scale cooperative game sample built on the new, Unity networking framework to teach developers about creating a similar multiplayer game.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found