question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When despawning a client owned object, the client attempts to send a NetworkVariableDeltaMessage when the NetworkObject is "null"

See original GitHub issue

Description

I have a situation where destroying a NetworkObject on the server causes the object to be deleted on the client while the clients NetworkBehaviourUpdater still tries to write its variables. This results in the following callstack being spammed repeatedly

MissingReferenceException: The object of type 'MyNetworkBehaviour' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UnityEngine.Component.GetComponentInParent (System.Type t, System.Boolean includeInactive) (at <b5cd1660e28545eca6b51cc4a296a19e>:0) UnityEngine.Component.GetComponentInParent[T] () (at <b5cd1660e28545eca6b51cc4a296a19e>:0) Unity.Netcode.NetworkBehaviour.get_NetworkObject () (at Library/PackageCache/com.unity.netcode.gameobjects@aac7ffb9ef/Runtime/Core/NetworkBehaviour.cs:326) Unity.Netcode.NetworkBehaviour.NetworkVariableUpdate (System.UInt64 targetClientId, System.Int32 behaviourIndex) (at Library/PackageCache/com.unity.netcode.gameobjects@aac7ffb9ef/Runtime/Core/NetworkBehaviour.cs:626) Unity.Netcode.NetworkBehaviour.VariableUpdate (System.UInt64 targetClientId) (at Library/PackageCache/com.unity.netcode.gameobjects@aac7ffb9ef/Runtime/Core/NetworkBehaviour.cs:598) Unity.Netcode.NetworkBehaviourUpdater.NetworkBehaviourUpdate (Unity.Netcode.NetworkManager networkManager) (at Library/PackageCache/com.unity.netcode.gameobjects@aac7ffb9ef/Runtime/Core/NetworkBehaviourUpdater.cs:61) Unity.Netcode.NetworkManager.OnNetworkManagerTick () (at Library/PackageCache/com.unity.netcode.gameobjects@aac7ffb9ef/Runtime/Core/NetworkManager.cs:1604) Unity.Netcode.NetworkTickSystem.UpdateTick (System.Double localTimeSec, System.Double serverTimeSec) (at Library/PackageCache/com.unity.netcode.gameobjects@aac7ffb9ef/Runtime/Timing/NetworkTickSystem.cs:102) Unity.Netcode.NetworkManager.OnNetworkPreUpdate () (at Library/PackageCache/com.unity.netcode.gameobjects@aac7ffb9ef/Runtime/Core/NetworkManager.cs:1568) Unity.Netcode.NetworkManager.NetworkUpdate (Unity.Netcode.NetworkUpdateStage updateStage) (at Library/PackageCache/com.unity.netcode.gameobjects@aac7ffb9ef/Runtime/Core/NetworkManager.cs:1476) Unity.Netcode.NetworkUpdateLoop.RunNetworkUpdateStage (Unity.Netcode.NetworkUpdateStage updateStage) (at Library/PackageCache/com.unity.netcode.gameobjects@aac7ffb9ef/Runtime/Core/NetworkUpdateLoop.cs:185) Unity.Netcode.NetworkUpdateLoop+NetworkPreUpdate+<>c.<CreateLoopSystem>b__0_0 () (at Library/PackageCache/com.unity.netcode.gameobjects@aac7ffb9ef/Runtime/Core/NetworkUpdateLoop.cs:232)

Reproduce Steps

I attempted to create a test project but was unable to reproduce it outside of my project. I think it is to do with a client updating a network variable when the server destroys the NetworkObject

Actual Outcome

When the server despawns the NetworkObject, the client still tries to send changed NetworkVariableDeltaMessages even though the NetworkObject is null

Expected Outcome

If an object is Despawned, the client stops interacting with the NetworkObject.

Environment

Additional Context

While trying to fix it myself, It seems that between NetworkBehaviourUpdater.AddForUpdate() and NetworkBehaviourUpdater.NetworkBehaviourUpdate() the NetworkObject becomes null

I have a “fix” in my branch which just makes sure to remove any null NetworkObject before running the update https://github.com/JayPeet/com.unity.netcode.gameobjects/commit/3c3b18a05882d36feabf199284eda3f7bf9b444b

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jeffreyrainycommented, Sep 9, 2022

Merged! Thanks.

0reactions
JayPeetcommented, Sep 10, 2022

That seemed to fix it for me. Thank you! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deferred messages were received for NetworkObject
I'm basically spawning prefabs with script attached to it, which after 1.5s sends request to server to Despawn the object. It actually works ......
Read more >
NetworkObject of my NetworkBehaviour is null for object in ...
StartGame I try to invoke an RPC on this NetworkBehaviour but I get InvalidOperationException: Behaviour not initialized: Object not set. I have ...
Read more >
NetworkObject | Unity Multiplayer Networking
Ownership. Either the server (default) or any connected and approved client each NetworkObject. Netcode for GameObjects is ...
Read more >
Destroying an object via despawn leads to a warning ...
Describe the bug Despawning an object leads to warning message "Trying to destroy object N, but it doesn't seem to exist anymore!
Read more >
Spawning and Despawning - Fish-Net: Networking Evolved
To spawn an object it must first be instantiated, and then passed into a Spawn method. There are a variety of Spawn methods...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found