[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 issueDescribe 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
- Enter play mode
- Host via IP, click on ‘host’
- Pick a character on CharSelect, wait for the main Boss Room scene to load
- Use the ‘go to main menu’ button within the Boss Room scene
- Attempt to host a new game via IP, click the ‘host’ button again.
- Error (
NullReferenceException
withinUnity.Netcode.Components.NetworkAnimator.OnClientConnectedCallback
).- The
NetworkManager.StartHost()
call succeeds, but the exception preventsCharSelect
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).
- The
Expected behavior The game should proceed to the ‘CharSelect’ scene, allowing the user to continue hosting the game as expected.
Screenshots
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 inCharSelect
andPostGame
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:
- Created a year ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
@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:
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).
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.