Skip to content

[Bug] NetworkManager immediate shutdown on StartHost (v2.11.1) due to NullReferenceException #3960

@AliasgarBohra

Description

@AliasgarBohra

Description

After upgrading to Netcode for GameObjects v2.11.1, calling StartHost() triggers an immediate shutdown of the NetworkManager. The issue stems from a NullReferenceException within NetworkConnectionManager.HandleConnectionApproval.

The error occurs at the following line:
var idHashToSpawn = playerPrefabHash ?? NetworkManager.NetworkConfig.PlayerPrefab?.GetComponent()?.GlobalObjectIdHash;

In my configuration, the Default Player Prefab is intentionally left null (as spawning is handled manually or conditionally). In previous versions, this configuration worked correctly. In v2.11.1, the system appears to attempt a component access on a null object without a sufficient null check, causing the initialization to fail and the transport to shut down.

Workaround: Downgrading to the previous version resolves the issue.

Full Trace:
NullReferenceException: Object reference not set to an instance of an object.
UnityEngine.Bindings.ThrowHelper.ThrowNullReferenceException (System.Object obj) (at <6567e2a645a94ef68fffcf0e5cb82d14>:0)
UnityEngine.GameObject.GetComponentFastPath (System.Type type, System.IntPtr oneFurtherThanResultValue) (at <6567e2a645a94ef68fffcf0e5cb82d14>:0)
UnityEngine.GameObject.GetComponent[T] () (at <6567e2a645a94ef68fffcf0e5cb82d14>:0)
Unity.Netcode.NetworkConnectionManager.HandleConnectionApproval (System.UInt64 ownerClientId, System.Boolean createPlayerObject, System.Nullable1[T] playerPrefabHash, System.Nullable1[T] playerPosition, System.Nullable1[T] playerRotation) (at ./Library/PackageCache/com.unity.netcode.gameobjects@53246f9d2516/Runtime/Connection/NetworkConnectionManager.cs:975) Unity.Netcode.NetworkManager.HostServerInitialize () (at ./Library/PackageCache/com.unity.netcode.gameobjects@53246f9d2516/Runtime/Core/NetworkManager.cs:1500) Unity.Netcode.NetworkManager.StartHost () (at ./Library/PackageCache/com.unity.netcode.gameobjects@53246f9d2516/Runtime/Core/NetworkManager.cs:1467) UnityEngine.Logger:LogException(Exception, Object) Unity.Netcode.Logging.ContextualLogger:Exception(Exception) Unity.Netcode.NetworkManager:StartHost() (at ./Library/PackageCache/com.unity.netcode.gameobjects@53246f9d2516/Runtime/Core/NetworkManager.cs:1472) ConnectionManager:StartHost(Boolean) (at Assets/Scripts/Connection/ConnectionManager.cs:46) <CreateLobby>d__14:MoveNext() (at Assets/Scripts/Connection/Lobby Transports/UnityLobbyRelayTransport.cs:100) System.Runtime.CompilerServices.AsyncTaskMethodBuilder1:SetResult(Boolean)
d__28:MoveNext() (at Assets/Scripts/Connection/Lobby Transports/UnityLobbyRelayTransport.cs:606)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder1:SetResult(Lobby) Unity.Services.Lobbies.Internal.<UpdatePlayerAsync>d__34:MoveNext() (at ./Library/PackageCache/com.unity.services.multiplayer@7c91ae1e57ac/Runtime/Lobbies/SDK/WrappedLobbyService.cs:407) System.Runtime.CompilerServices.AsyncTaskMethodBuilder1:SetResult(Response1) Unity.Services.Lobbies.Internal.<TryCatchRequest>d__422:MoveNext() (at ./Library/PackageCache/com.unity.services.multiplayer@7c91ae1e57ac/Runtime/Lobbies/SDK/WrappedLobbyService.cs:609)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder1:SetResult(Response1)
Unity.Services.Lobbies.Apis.Lobby.d__24:MoveNext() (at ./Library/PackageCache/com.unity.services.multiplayer@7c91ae1e57ac/Runtime/Lobbies/Apis/LobbyApi.cs:732)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder1:SetResult(HttpClientResponse) Unity.Services.Lobbies.Http.<MakeRequestAsync>d__1:MoveNext() (at ./Library/PackageCache/com.unity.services.multiplayer@7c91ae1e57ac/Runtime/Lobbies/Http/HttpClient.cs:41) System.Runtime.CompilerServices.AsyncTaskMethodBuilder1:SetResult(HttpClientResponse)
Unity.Services.Lobbies.Http.d__3:MoveNext() (at ./Library/PackageCache/com.unity.services.multiplayer@7c91ae1e57ac/Runtime/Lobbies/Http/HttpClient.cs:56)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder1:SetResult(HttpClientResponse) Unity.Services.Lobbies.Http.<CreateHttpClientResponse>d__4:MoveNext() (at ./Library/PackageCache/com.unity.services.multiplayer@7c91ae1e57ac/Runtime/Lobbies/Http/HttpClient.cs:84) System.Runtime.CompilerServices.AsyncTaskMethodBuilder1:SetResult(HttpClientResponse)
Unity.Services.Lobbies.Http.<b__0>d:MoveNext() (at ./Library/PackageCache/com.unity.services.multiplayer@7c91ae1e57ac/Runtime/Lobbies/Http/HttpClient.cs:81)
System.Threading.Tasks.TaskCompletionSource`1:SetResult(HttpClientResponse)
Unity.Services.Lobbies.Http.<>c__DisplayClass0_0:b__0(AsyncOperation) (at ./Library/PackageCache/com.unity.services.multiplayer@7c91ae1e57ac/Runtime/Lobbies/Http/UnityWebRequestHelpers.cs:34)
UnityEngine.AsyncOperation:InvokeCompletionEvent()

Directly calling UnityTransport.Shutdown() results in unexpected shutdown behaviour. All pending events will be lost. Use NetworkManager.Shutdown() instead.
UnityEngine.Debug:LogWarning (object)
Unity.Netcode.Transports.UTP.UnityTransport:Shutdown () (at ./Library/PackageCache/com.unity.netcode.gameobjects@53246f9d2516/Runtime/Transports/UTP/UnityTransport.cs:1727)
Unity.Netcode.NetworkConnectionManager:Shutdown () (at ./Library/PackageCache/com.unity.netcode.gameobjects@53246f9d2516/Runtime/Connection/NetworkConnectionManager.cs:1719)
Unity.Netcode.NetworkManager:ShutdownInternal () (at ./Library/PackageCache/com.unity.netcode.gameobjects@53246f9d2516/Runtime/Core/NetworkManager.cs:1651)
Unity.Netcode.NetworkManager:StartHost () (at ./Library/PackageCache/com.unity.netcode.gameobjects@53246f9d2516/Runtime/Core/NetworkManager.cs:1474)
ConnectionManager:StartHost (bool) (at Assets/Scripts/Connection/ConnectionManager.cs:46)
UnityLobbyRelayTransport/d__14:MoveNext () (at Assets/Scripts/Connection/Lobby Transports/UnityLobbyRelayTransport.cs:100)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder1<bool>:SetResult (bool) UnityLobbyRelayTransport/<UpdatePlayerData>d__28:MoveNext () (at Assets/Scripts/Connection/Lobby Transports/UnityLobbyRelayTransport.cs:606) System.Runtime.CompilerServices.AsyncTaskMethodBuilder1<Unity.Services.Lobbies.Models.Lobby>:SetResult (Unity.Services.Lobbies.Models.Lobby)
Unity.Services.Lobbies.Internal.WrappedLobbyService/d__34:MoveNext () (at ./Library/PackageCache/com.unity.services.multiplayer@7c91ae1e57ac/Runtime/Lobbies/SDK/WrappedLobbyService.cs:407)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder1<Unity.Services.Lobbies.Response1<Unity.Services.Lobbies.Models.Lobby>>:SetResult (Unity.Services.Lobbies.Response1<Unity.Services.Lobbies.Models.Lobby>) Unity.Services.Lobbies.Internal.WrappedLobbyService/<TryCatchRequest>d__422<Unity.Services.Lobbies.Lobby.UpdatePlayerRequest, Unity.Services.Lobbies.Models.Lobby>:MoveNext () (at ./Library/PackageCache/com.unity.services.multiplayer@7c91ae1e57ac/Runtime/Lobbies/SDK/WrappedLobbyService.cs:609)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder1<Unity.Services.Lobbies.Response1<Unity.Services.Lobbies.Models.Lobby>>:SetResult (Unity.Services.Lobbies.Response1<Unity.Services.Lobbies.Models.Lobby>) Unity.Services.Lobbies.Apis.Lobby.LobbyApiClient/<UpdatePlayerAsync>d__24:MoveNext () (at ./Library/PackageCache/com.unity.services.multiplayer@7c91ae1e57ac/Runtime/Lobbies/Apis/LobbyApi.cs:732) System.Runtime.CompilerServices.AsyncTaskMethodBuilder1<Unity.Services.Lobbies.Http.HttpClientResponse>:SetResult (Unity.Services.Lobbies.Http.HttpClientResponse)
Unity.Services.Lobbies.Http.HttpClient/d__1:MoveNext () (at ./Library/PackageCache/com.unity.services.multiplayer@7c91ae1e57ac/Runtime/Lobbies/Http/HttpClient.cs:41)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder1<Unity.Services.Lobbies.Http.HttpClientResponse>:SetResult (Unity.Services.Lobbies.Http.HttpClientResponse) Unity.Services.Lobbies.Http.HttpClient/<CreateWebRequestAsync>d__3:MoveNext () (at ./Library/PackageCache/com.unity.services.multiplayer@7c91ae1e57ac/Runtime/Lobbies/Http/HttpClient.cs:56) System.Runtime.CompilerServices.AsyncTaskMethodBuilder1<Unity.Services.Lobbies.Http.HttpClientResponse>:SetResult (Unity.Services.Lobbies.Http.HttpClientResponse)
Unity.Services.Lobbies.Http.HttpClient/d__4:MoveNext () (at ./Library/PackageCache/com.unity.services.multiplayer@7c91ae1e57ac/Runtime/Lobbies/Http/HttpClient.cs:84)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder1<Unity.Services.Lobbies.Http.HttpClientResponse>:SetResult (Unity.Services.Lobbies.Http.HttpClientResponse) Unity.Services.Lobbies.Http.HttpClient/<>c__DisplayClass4_0/<<CreateHttpClientResponse>b__0>d:MoveNext () (at ./Library/PackageCache/com.unity.services.multiplayer@7c91ae1e57ac/Runtime/Lobbies/Http/HttpClient.cs:81) System.Threading.Tasks.TaskCompletionSource1<Unity.Services.Lobbies.Http.HttpClientResponse>:SetResult (Unity.Services.Lobbies.Http.HttpClientResponse)
Unity.Services.Lobbies.Http.UnityWebRequestHelpers/<>c__DisplayClass0_0:b__0 (UnityEngine.AsyncOperation) (at ./Library/PackageCache/com.unity.services.multiplayer@7c91ae1e57ac/Runtime/Lobbies/Http/UnityWebRequestHelpers.cs:34)
UnityEngine.AsyncOperation:InvokeCompletionEvent ()

Reproduce Steps

  1. Set the Player Prefab in the NetworkManager configuration to None (Null).

  2. Call NetworkManager.Singleton.StartHost().

  3. Observe the NullReferenceException in the console followed by an immediate NetworkManager shutdown.

Actual Outcome

The NetworkManager throws a NullReferenceException during the host initialization sequence and performs an emergency shutdown.

Expected Outcome

The Host should start successfully even if the default PlayerPrefab is null, or it should gracefully handle the null reference without crashing the manager, consistent with behavior in previous versions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    stat:awaiting-triageStatus - Awaiting triage from the Netcode team.stat:reply-neededAwaiting reply from Unity accounttype:bugBug Report

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions