Skip to content

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

Open
maxkratt wants to merge 1 commit into
FirstGearGames:mainfrom
maxkratt:nob-serializer-bug-fix
Open

maxkratt wants to merge 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.

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.

1 participant