Hi,
I am using NGO and Multiplayer Play Mode Tool to test my game. I have a simple scene with UI buttons for stating host and client. My problem is when i start the client the host throws an error that is seems to be a unity thing.
The error is this:
InvalidOperationException: Nullable object must have a value.
System.Nullable`1[T].get_Value () (at <321eb2db7c6d43ea8fc39b54eaca3452>:0)
Unity.Netcode.NetworkObject.HostCheckForGlobalObjectIdHashOverride () (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs:3258)
Unity.Netcode.NetworkObject.GetMessageSceneObject (System.UInt64 targetClientId, System.Boolean syncObservers) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs:2873)
Unity.Netcode.SceneEventData.WriteSceneSynchronizationData (Unity.Netcode.FastBufferWriter writer) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/SceneManagement/SceneEventData.cs:612)
Unity.Netcode.SceneEventData.Serialize (Unity.Netcode.FastBufferWriter writer) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/SceneManagement/SceneEventData.cs:518)
Unity.Netcode.SceneEventMessage.Serialize (Unity.Netcode.FastBufferWriter writer, System.Int32 targetVersion) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Messaging/Messages/SceneEventMessage.cs:17)
Unity.Netcode.NetworkMessageManager.SendMessage[TMessageType,TClientIdListType] (TMessageType& message, Unity.Netcode.NetworkDelivery delivery, TClientIdListType& clientIds) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Messaging/NetworkMessageManager.cs:644)
Unity.Netcode.NetworkMessageManager.SendMessage[T] (T& message, Unity.Netcode.NetworkDelivery delivery, System.UInt64 clientId) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Messaging/NetworkMessageManager.cs:821)
Unity.Netcode.NetworkConnectionManager.SendMessage[T] (T& message, Unity.Netcode.NetworkDelivery delivery, System.UInt64 clientId) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs:1540)
Unity.Netcode.NetworkSceneManager.SynchronizeNetworkObjects (System.UInt64 clientId) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs:2075)
Unity.Netcode.NetworkConnectionManager.HandleConnectionApproval (System.UInt64 ownerClientId, Unity.Netcode.NetworkManager+ConnectionApprovalResponse response) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs:833)
Unity.Netcode.ConnectionRequestMessage.Handle (Unity.Netcode.NetworkContext& context) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Messaging/Messages/ConnectionRequestMessage.cs:166)
Unity.Netcode.NetworkMessageManager.ReceiveMessage[T] (Unity.Netcode.FastBufferReader reader, Unity.Netcode.NetworkContext& context, Unity.Netcode.NetworkMessageManager manager) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Messaging/NetworkMessageManager.cs:585)
Unity.Netcode.NetworkMessageManager.HandleMessage (Unity.Netcode.NetworkMessageHeader& header, Unity.Netcode.FastBufferReader reader, System.UInt64 senderId, System.Single timestamp, System.Int32 serializedHeaderSize) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Messaging/NetworkMessageManager.cs:444)
UnityEngine.Debug:LogException(Exception)
Unity.Netcode.NetworkMessageManager:HandleMessage(NetworkMessageHeader&, FastBufferReader, UInt64, Single, Int32) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Messaging/NetworkMessageManager.cs:448)
Unity.Netcode.NetworkMessageManager:ProcessIncomingMessageQueue() (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Messaging/NetworkMessageManager.cs:470)
Unity.Netcode.NetworkManager:NetworkUpdate(NetworkUpdateStage) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs:288)
Unity.Netcode.NetworkUpdateLoop:RunNetworkUpdateStage(NetworkUpdateStage) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkUpdateLoop.cs:191)
Unity.Netcode.<>c:<CreateLoopSystem>b__0_0() (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkUpdateLoop.cs:214)
My NetworkManager
Right now I am spawning the players with a custom function . When I use the NetworkManager to spawn them the error does not appear.
This is my function:
private void NetworkManager_OnClientConnectedCallback(ulong obj) {
if (!IsHost) return;
var playerInstance = Instantiate(playerPrefab, playerSpawns[(int)obj].position, playerSpawns[(int)obj].rotation);
NetworkObject instanceNetworkObject = playerInstance.GetComponent<NetworkObject>();
instanceNetworkObject.SpawnAsPlayerObject(obj,true);
Debug.Log(obj + " player connected");
playerSpawnChannel.RaiseEvent(instanceNetworkObject);
}
This was working fine!!!
Any help is appreciated.
I have a hunch that it’s either this, or perhaps you are not supposed to spawn network objects from within a (client connected) callback. Try deferring the execution by adding a coroutine in between that yields null before it runs the spawn code.
It’s also curious that you raise an event to the spawn channel. If this is a network message channel, it’s superfluous to do so. You can as well use the player object’s OnNetworkSpawn to send the “player spawned” event locally on each client.
I moved the event and now player spawns. I cant understand how this line was preventing the client from connecting to the server? When I was debbuging with breakpoints my function NetworkManager_OnClientConnectedCallback was never called. The thing that bothers me is that all this code was working fine and suddenly didn’t .
Also now when I stop play mode host throws another error.
NullReferenceException: Object reference not set to an instance of an object
Unity.Netcode.NetworkSpawnManager.ChangeOwnership (Unity.Netcode.NetworkObject networkObject, System.UInt64 clientId, System.Boolean isAuthorized, System.Boolean isRequestApproval) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs:501)
Unity.Netcode.NetworkSpawnManager.RemoveOwnership (Unity.Netcode.NetworkObject networkObject) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs:423)
Unity.Netcode.NetworkObject.RemoveOwnership () (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs:1743)
Unity.Netcode.NetworkConnectionManager.OnClientDisconnectFromServer (System.UInt64 clientId) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs:1104)
Unity.Netcode.NetworkConnectionManager.DisconnectRemoteClient (System.UInt64 clientId) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs:1290)
Unity.Netcode.NetworkConnectionManager.Shutdown () (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs:1398)
Unity.Netcode.NetworkManager.ShutdownInternal () (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs:1558)
Unity.Netcode.NetworkManager.ModeChanged (UnityEditor.PlayModeStateChange change) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs:979)
UnityEditor.EditorApplication.Internal_PlayModeStateChanged (UnityEditor.PlayModeStateChange state) (at <1f8babe234e540299e4f7387cdc20cd8>:0)
UnityEditor.EditorApplication:ExitPlaymode()
Unity.Multiplayer.PlayMode.Configurations.Editor.DefaultPlayModeConfig:ExecuteStop() (at ./Library/PackageCache/com.unity.multiplayer.playmode/Configurations/Editor/DefaultPlayModeConfig.cs:18)
Unity.Multiplayer.PlayMode.Configurations.Editor.PlayModeManager:TransitionActionAsync(PlayModeState, PlayModeState, CancellationToken) (at ./Library/PackageCache/com.unity.multiplayer.playmode/Configurations/Editor/PlayModeManager.cs:201)
Unity.Multiplayer.PlayMode.Configurations.Editor.<TransitionAsync>d__18:MoveNext() (at ./Library/PackageCache/com.unity.multiplayer.playmode/Configurations/Editor/StateMachine.cs:135)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder:Start(<TransitionAsync>d__18&)
Unity.Multiplayer.PlayMode.Configurations.Editor.StateMachine`1:TransitionAsync(PlayModeState, Func`4, CancellationToken)
Unity.Multiplayer.PlayMode.Configurations.Editor.PlayModeManager:TransitionToState(PlayModeState) (at ./Library/PackageCache/com.unity.multiplayer.playmode/Configurations/Editor/PlayModeManager.cs:191)
Unity.Multiplayer.PlayMode.Configurations.Editor.PlayModeManager:Stop() (at ./Library/PackageCache/com.unity.multiplayer.playmode/Configurations/Editor/PlayModeManager.cs:172)
Unity.Multiplayer.PlayMode.Configurations.Editor.Gui.PlayModeButtonsView:OnPlayButtonValueChanged(ChangeEvent`1) (at ./Library/PackageCache/com.unity.multiplayer.playmode/Configurations/Editor/GUI/PlayModeButtonsView.cs:194)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)
Any idea what is this error ?
Xebain
March 31, 2025, 10:13pm
4
This is a relatively old thread, but I think this may be useful for anyone running into a similar issue to save them from the hours of head-bashing that I went through trying to debug this.
InvalidOperationException: Nullable object must have a value.
System.Nullable`1[T].get_Value () (at <321eb2db7c6d43ea8fc39b54eaca3452>:0)
Unity.Netcode.NetworkObject.HostCheckForGlobalObjectIdHashOverride () (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs:3258)
Unity.Netcode.NetworkObject.GetMessageSceneObject (System.UInt64 targetClientId, System.Boolean syncObservers) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs:2873)
Unity.Netcode.SceneEventData.WriteSceneSynchronizationData (Unity.Netcode.FastBufferWriter writer) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/SceneManagement/SceneEventData.cs:612)
Unity.Netcode.SceneEventData.Serialize (Unity.Netcode.FastBufferWriter writer) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/SceneManagement/SceneEventData.cs:518)
Unity.Netcode.SceneEventMessage.Serialize (Unity.Netcode.FastBufferWriter writer, System.Int32 targetVersion) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Messaging/Messages/SceneEventMessage.cs:17)
Unity.Netcode.NetworkMessageManager.SendMessage[TMessageType,TClientIdListType] (TMessageType& message, Unity.Netcode.NetworkDelivery delivery, TClientIdListType& clientIds) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Messaging/NetworkMessageManager.cs:644)
Unity.Netcode.NetworkMessageManager.SendMessage[T] (T& message, Unity.Netcode.NetworkDelivery delivery, System.UInt64 clientId) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Messaging/NetworkMessageManager.cs:821)
Unity.Netcode.NetworkConnectionManager.SendMessage[T] (T& message, Unity.Netcode.NetworkDelivery delivery, System.UInt64 clientId) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs:1540)
Unity.Netcode.NetworkSceneManager.SynchronizeNetworkObjects (System.UInt64 clientId) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs:2075)
Unity.Netcode.NetworkConnectionManager.HandleConnectionApproval (System.UInt64 ownerClientId, Unity.Netcode.NetworkManager+ConnectionApprovalResponse response) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs:833)
Unity.Netcode.ConnectionRequestMessage.Handle (Unity.Netcode.NetworkContext& context) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Messaging/Messages/ConnectionRequestMessage.cs:166)
Unity.Netcode.NetworkMessageManager.ReceiveMessage[T] (Unity.Netcode.FastBufferReader reader, Unity.Netcode.NetworkContext& context, Unity.Netcode.NetworkMessageManager manager) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Messaging/NetworkMessageManager.cs:585)
Unity.Netcode.NetworkMessageManager.HandleMessage (Unity.Netcode.NetworkMessageHeader& header, Unity.Netcode.FastBufferReader reader, System.UInt64 senderId, System.Single timestamp, System.Int32 serializedHeaderSize) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Messaging/NetworkMessageManager.cs:444)
UnityEngine.Debug:LogException(Exception)
Unity.Netcode.NetworkMessageManager:HandleMessage(NetworkMessageHeader&, FastBufferReader, UInt64, Single, Int32) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Messaging/NetworkMessageManager.cs:448)
Unity.Netcode.NetworkMessageManager:ProcessIncomingMessageQueue() (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Messaging/NetworkMessageManager.cs:470)
Unity.Netcode.NetworkManager:NetworkUpdate(NetworkUpdateStage) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs:288)
Unity.Netcode.NetworkUpdateLoop:RunNetworkUpdateStage(NetworkUpdateStage) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkUpdateLoop.cs:191)
Unity.Netcode.<>c:<CreateLoopSystem>b__0_0() (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkUpdateLoop.cs:214)
For me, this issue was caused by an unspawned NetworkObject that was attempting to be serialized by the host when a client joined. This happened due to Unity auto-adding the NetworkObject component to scripts deriving from NetworkBehaviour, which is typically great. This isn’t so great when you decide swap the derivation to MonoBehaviour while also forgetting about Unity automatically adding that NetworkObject component (or when trying to adjust objects with NetworkBehaviours in inspector and getting absolutely spam f’ed with the “NetworkBehaviours require a NetworkObject” popup, but that’s the extremely annoying besides-the-point).
In short, if you run into this error message, check to make sure that whatever is being serialized is a spawned NetworkObject or is being handled by a spawned NetworkObject. Failing to do so will create severe headaches.