Skip to content

NetworkRigidbody2D throws NullReferenceException on spawn when Rigidbody2D interpolation is Extrapolate #3924

@ercantomac

Description

@ercantomac

Description

I ran into a reproducible NullReferenceException when using NetworkRigidbody2D with a Rigidbody2D whose interpolation mode is set to Extrapolate.

This happens during multiplayer spawning on the non-owner/client side. Changing the same Rigidbody2D interpolation setting from Extrapolate to Interpolate makes the issue disappear immediately.

This makes it look like a package bug in NGO rather than a project-side null reference.

Versions

  • Netcode for GameObjects: 2.11.0
  • Unity Transport: 2.6.0
  • Multiplayer Services: 2.0.0

Reproduction Steps

  1. Create a networked prefab with:
    • NetworkObject
    • NetworkTransform
    • Rigidbody2D
    • NetworkRigidbody2D
  2. Set the prefab's Rigidbody2D.interpolation to Extrapolate.
  3. Spawn the prefab in a multiplayer session (in my case as a player object).
  4. Connect a client / let the non-owner instance spawn.

Expected Result

The object should spawn normally, and NGO should handle NetworkRigidbody2D without errors.

Actual Result

A NullReferenceException is thrown during spawn/initialization.

Stack Trace

NullReferenceException: Object reference not set to an instance of an object
Unity.Netcode.Components.NetworkRigidbodyBase.UpdateOwnershipAuthority () (at ./Library/PackageCache/com.unity.netcode.gameobjects@02e4aaa4170c/Runtime/Components/NetworkRigidBodyBase.cs:993)
Unity.Netcode.Components.NetworkTransform.InternalInitialization (System.Boolean isOwnershipChange) (at ./Library/PackageCache/com.unity.netcode.gameobjects@02e4aaa4170c/Runtime/Components/NetworkTransform.cs:3743)
Unity.Netcode.Components.NetworkTransform.Initialize () (at ./Library/PackageCache/com.unity.netcode.gameobjects@02e4aaa4170c/Runtime/Components/NetworkTransform.cs:3809)
Unity.Netcode.Components.NetworkTransform.OnNetworkSpawn () (at ./Library/PackageCache/com.unity.netcode.gameobjects@02e4aaa4170c/Runtime/Components/NetworkTransform.cs:3612)
Unity.Netcode.NetworkBehaviour.NetworkSpawn () (at ./Library/PackageCache/com.unity.netcode.gameobjects@02e4aaa4170c/Runtime/Core/NetworkBehaviour.cs:814)

Workaround

Changing Rigidbody2D.interpolation from Extrapolate to Interpolate fixes the problem.

Additional Notes

While investigating this locally, I noticed code in NetworkRigidBodyBase that appears to access the 3D rigidbody field during the extrapolation path:

if (UseRigidBodyForMotion)
{
    if (NetworkTransform.Interpolate && m_OriginalInterpolation == InterpolationTypes.Extrapolate)
    {
        if (IsKinematic())
        {
            if (m_InternalRigidbody.interpolation == RigidbodyInterpolation.Extrapolate)

For NetworkRigidbody2D, this looks suspicious because the 2D path should not be relying on m_InternalRigidbody.

Metadata

Metadata

Assignees

No one assigned

    Labels

    stat:awaiting-triageStatus - Awaiting triage from the Netcode team.stat:reply-neededAwaiting reply from Unity accounttype:bugBug Report

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions