com.unity.netcode.gameobjects
com.unity.netcode.gameobjects copied to clipboard
Null ref exception thrown if a network variable is written to as we are exiting the scene
If we happen to write to a network variable at the same time as we're leaving the scene, we get a null ref exception GameObjects (including networked ones) are being destroyed as we're leaving the scene.
NullReferenceException: Object reference not set to an instance of an object
at Unity.Netcode.NetworkVariableBase.MarkNetworkBehaviourDirty () [0x00034] in <e8b815437985499482c06f5e440e5efb>:0
at Unity.Netcode.NetworkVariableBase.SetDirty (System.Boolean isDirty) [0x0000f] in <e8b815437985499482c06f5e440e5efb>:0
at Unity.Netcode.NetworkVariable`1[T].Set (T value) [0x00000] in <e8b815437985499482c06f5e440e5efb>:0
at Unity.Netcode.NetworkVariable`1[T].set_Value (T value) [0x00045] in <e8b815437985499482c06f5e440e5efb>:0
at Assets.Scripts.MyProject.Game.GameTime+<IncrementGameTime>d__7.MoveNext () [0x0004a] in <259ca83a0e754ab58e100f04423a0562>:0
at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00026] in <cb1059a94bbc49b4896b7dad8be3a918>:0
Reproduce Steps
- Create a network variable
- Write to this network variable frequently, in a away where there's a good chance that we will try and write to it as we leave our application.
- Exit the application.
Actual Outcome
A null ref exception is thrown.
Expected Outcome
No error is thrown.
Environment
- OS: Windows 10
- Unity Version: 2023.2.5
- Netcode Version: 1.7.1
Hi @TheCaveOfWonders Please download and install [email protected] and test to see if the issue is resolved on your end.
If you continue to experience any problems, please report back to this ticket so we can investigate further.
Thank you for your patience and cooperation!
I'm having the same error as well. I updated from 1.8.1 to 1.10.0 as @fluong6 suggested but it still gave me the same error:
NullReferenceException: Object reference not set to an instance of an object
Unity.Netcode.NetworkVariable`1[T].set_Value (T value) (at ./Library/PackageCache/[email protected]/Runtime/NetworkVariable/NetworkVariable.cs:94)
Cargo+<Countdown>d__13.MoveNext () (at Assets/Resources/Gamemodes/FreeForAll/Cargo/Cargo.cs:46)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <228a75ed9d5b4a658c89f48527e7e2e0>:0)
@TheCaveOfWonders & @Conundroy
This issue pertains to writing to a NetworkVariable when the NetworkObject is no longer spawned and most likely being destroyed. Under both conditions, writing to a NetworkVariable during that time will result in that information not being synchronized nor persisted.
With that said, I am pretty sure the updates in v1.11.0 should resolve this issue.
Not knowing the exact conditions of the scene being unloaded and what else might be getting destroyed, if you still get this exception (@Conundroy specifically at that line number) then it is very likely that the NetworkBehaviour component is in the middle of being destroyed. The best solution, under that scenario would be to check if the NetworkBehaviour.IsSpawned is still true before setting the value.