Skip to content

NullReferenceException in SceneEventData.DeserializeScenePlacedObjects() when NetworkObject.Deserialize returns null #4083

Description

@AliasgarBohra

Description

NullReferenceException is thrown in SceneEventData.DeserializeScenePlacedObjects() when NetworkObject.Deserialize() returns null for an in-scene placed NetworkObject during scene synchronization. The null is added to a list without a check, and then InternalInSceneNetworkObjectsSpawned() is called on it, crashing and aborting the entire scene sync for that client.

Reproduce Steps

  1. Host or start a server with a scene containing multiple in-scene placed NetworkObjects
  2. Have a client connect and load the scene (can also occur on a Load scene event during gameplay)
  3. Under certain timing/race conditions, NetworkObject.Deserialize() returns null for one of the in-scene placed NetworkObjects (e.g., when GetSceneRelativeInSceneNetworkObject fails to find the object by GlobalObjectIdHash on the client at sync time)
  4. The null reference is added to the sceneObjects list without a null check
  5. The foreach loop calls networkObject.InternalInSceneNetworkObjectsSpawned() on the null entry → NullReferenceException

The issue is intermittent — it does not happen every time. It occurs randomly across different game modes and scenes, but once it triggers, the client's scene is permanently broken.

Actual Outcome

NullReferenceException: Object reference not set to an instance of an object
Unity.Netcode.SceneEventData.DeserializeScenePlacedObjects () (at Library/PackageCache/com.unity.netcode.gameobjects@aaabf07f880c/Runtime/SceneManagement/SceneEventData.cs:895)

The exception aborts DeserializeScenePlacedObjects mid-loop. Remaining in-scene placed NetworkObjects never get spawned. The client ends up in a corrupted state:

  • In-scene NetworkObjects are missing or partially spawned
  • Player spawns fail for that client
  • The scene is broken with no recovery path short of reconnecting

Expected Outcome

If NetworkObject.Deserialize() returns null for a specific in-scene placed NetworkObject, that entry should be skipped (not added to the sceneObjects list). The remaining in-scene placed NetworkObjects should continue to synchronize normally. No NullReferenceException should be thrown.

Screenshots

N/A — error is in console/log output only.

Environment

  • OS: Windows 11
  • Unity Version: 6000.3.19
  • Netcode Version: 2.12.0
  • Netcode Commit: aaabf07f880c (package hash from PackageCache)
  • Netcode Topology: Client-Server

Additional Context

Root cause is in SceneEventData.DeserializeScenePlacedObjects():

// Line 881: Deserialize can return null
var networkObject = NetworkObject.Deserialize(serializedObject, InternalBuffer, m_NetworkManager);

// Line 883-886: BUG — no null check before adding to list
if (serializedObject.IsSceneObject)
{
    sceneObjects.Add(networkObject);  // networkObject can be null here
}

// Line 893-896: Crashes on null
foreach (var networkObject in sceneObjects)
{
    networkObject.InternalInSceneNetworkObjectsSpawned();  // NullReferenceException
}

NetworkObject.Deserialize() returns null in several cases:

  • NonAuthorityLocalSpawn fails (line 1217-1218: duplicate NetworkObjectId → returns null)
  • CreateLocalNetworkObject returns null (line 978-985: GetSceneRelativeInSceneNetworkObject fails to find the in-scene object by hash)
  • SpawnNetworkObjectLocallyCommon returns false (line 1284-1288)

None of these null returns are guarded before being added to sceneObjects.

Suggested fix (one-line change):

// Change line 883 from:
if (serializedObject.IsSceneObject)

// To:
if (serializedObject.IsSceneObject && networkObject != null)

This prevents null entries from entering the list, allowing the rest of the scene sync to complete normally.

Note: This issue was previously reported as #3795 but was closed as stale without a fix. It is still reproducible in 2.12.0 and is not addressed in 2.13.0. The impact is severe — a single failed object deserialization corrupts the entire client scene state with no recovery.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions