Unity-Technologies / com.unity.netcode.gameobjects

Netcode for GameObjects is a high-level netcode SDK that provides networking capabilities to GameObject/MonoBehaviour workflows within Unity and sits on top of underlying transport layer.
MIT License
2.16k stars 435 forks source link

Null ref exception thrown if a network variable is written to as we are exiting the scene #2811

Open TheCaveOfWonders opened 10 months ago

TheCaveOfWonders commented 10 months ago

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

  1. Create a network variable
  2. 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.
  3. Exit the application.

Actual Outcome

A null ref exception is thrown.

Expected Outcome

No error is thrown.

Environment

fluong6 commented 4 months ago

Hi @TheCaveOfWonders Please download and install com.unity.netcode.gameobjects@1.10.0 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!

Conundroy commented 3 months ago

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/com.unity.netcode.gameobjects@1.10.0/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)
NoelStephensUnity commented 1 month ago

@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.