Skip to content

fix(serializing): write null for despawned NetworkObject references - #1078

Merged
FirstGearGames merged 1 commit into
FirstGearGames:mainfrom
maxkratt:nob-serializer-bug-fix
Oct 9, 2026
Merged

FirstGearGames merged 1 commit into
FirstGearGames:mainfrom
maxkratt:nob-serializer-bug-fix

Conversation

@maxkratt

Copy link
Copy Markdown
Contributor

Problem

Writing a NetworkObject that had been despawned sent its PrefabId, so the
reader resolved the reference to the prefab asset instead of null. This
affected RPC parameters, SyncVars, broadcasts and any custom serializer,
through WriteNetworkObject, WriteNetworkBehaviour, WriteGameObject and
WriteTransform.

The receiver ends up holding the prefab asset where it should have null, so code acting on that reference could end up operating on the prefab instead of an instance. This is easy to hit: despawn an object and pass it to an RPC or SyncVar in the same frame. Pooled objects are affected for as long as a stale reference is held, not just for that frame.

Cause

Despawning sets IsDeinitializing, so IsSpawned becomes false while the
C# reference stays non-null. WriteNetworkObject took its "not spawned"
branch, which exists for prefab-template references, and wrote PrefabId.

Fix

NetworkManager is assigned when a NetworkObject initializes and is never
cleared by ResetState. If it is set, the object was spawned at some point
and is no longer valid, so a null reference is written instead. Prefab
assets and never-spawned instances still write their PrefabId.

Behavior change

A recycled pooled instance that has been fetched from
the pool but not yet spawned now serializes as null instead of the
prefab.

@FirstGearGames
FirstGearGames merged commit b23810b into FirstGearGames:main Oct 9, 2026
FirstGearGames pushed a commit that referenced this pull request Oct 16, 2026
- Fixed prefab collection generator excluded folders not matching when saved on another platform or with a trailing slash (#1080).
- Fixed SetLowestMTUs using the last channel's MTU instead of the lowest (#1101).
- Fixed split message pieces being appended to non-empty buffers (#1079).
- Fixed late-joining clients using the most recent global load's replace option instead of the last replacing load (#1035).
- Fixed client prediction TimeManager.OnUpdate subscriptions stacking on pooled objects (#1075).
- Fixed WriteInt64Unpacked writing a packed value instead of 8 bytes (#1072).
- Fixed runtime parent not being set on clients for instantiated spawns (#1058).
- Fixed instantiated spawns parented to a non-first NetworkBehaviour being placed under the wrong transform (#1058).
- Fixed nested scene objects reparenting directly under their parent NetworkObject on clients (#1048).
- Fixed runtime unparenting of scene objects not reaching clients (#1048).
- Fixed obsolete Unity find and scripting-define calls behind version defines; compiles clean on Unity 6.6.
- Fixed jagged arrays being rejected as multidimensional in codegen (#1105).
- Fixed nullable arrays generating an open Nullable type in codegen (#1105).
- Fixed clientHost scene object lookup for scenes loaded more than once (#1081).
- Fixed despawned or pooled NetworkObjects not being written as a null reference (#1078).
- Fixed NetworkCollision2D being commented out (#1074).
- Fixed TimeManager rewriting the project's physics simulation mode and fixed delta time outside play mode (#1077).
- Fixed network HUD canvas buttons being hidden when using the new input system (#1082).
- Fixed OnDespawnServer not running for a connection removed by an observer rebuild while disconnecting (#1107).
- Added level of detail to NetworkTransform.
- Added TimeManager.NormalizedDeltaTime so OnUpdate movement covers the same distance every tick.
- Added NetworkNavMeshAgent, synchronizing a NavMeshAgent.
FirstGearGames pushed a commit that referenced this pull request Oct 18, 2026
- Fixed prefab collection generator excluded folders not matching when saved on another platform or with a trailing slash (#1080).
- Fixed SetLowestMTUs using the last channel's MTU instead of the lowest (#1101).
- Fixed split message pieces being appended to non-empty buffers (#1079).
- Fixed late-joining clients using the most recent global load's replace option instead of the last replacing load (#1035).
- Fixed client prediction TimeManager.OnUpdate subscriptions stacking on pooled objects (#1075).
- Fixed WriteInt64Unpacked writing a packed value instead of 8 bytes (#1072).
- Fixed runtime parent not being set on clients for instantiated spawns (#1058).
- Fixed instantiated spawns parented to a non-first NetworkBehaviour being placed under the wrong transform (#1058).
- Fixed nested scene objects reparenting directly under their parent NetworkObject on clients (#1048).
- Fixed runtime unparenting of scene objects not reaching clients (#1048).
- Fixed obsolete Unity find and scripting-define calls behind version defines; compiles clean on Unity 6.6.
- Fixed jagged arrays being rejected as multidimensional in codegen (#1105).
- Fixed nullable arrays generating an open Nullable type in codegen (#1105).
- Fixed clientHost scene object lookup for scenes loaded more than once (#1081).
- Fixed despawned or pooled NetworkObjects not being written as a null reference (#1078).
- Fixed NetworkCollision2D being commented out (#1074).
- Fixed TimeManager rewriting the project's physics simulation mode and fixed delta time outside play mode (#1077).
- Fixed network HUD canvas buttons being hidden when using the new input system (#1082).
- Fixed OnDespawnServer not running for a connection removed by an observer rebuild while disconnecting (#1107).
- Added level of detail to NetworkTransform.
- Added TimeManager.NormalizedDeltaTime so OnUpdate movement covers the same distance every tick.
- Added NetworkNavMeshAgent, synchronizing a NavMeshAgent.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants