Error sending message: Connection ID is invalid. Likely caused by sending on stale connection 0. 0x0

Hello Unity Community,

I hope this message finds you well. I’m reaching out because I’ve encountered a perplexing issue in my Unity project that has appeared out of nowhere. I’m hoping to get some insights and possibly report a bug if necessary.

The Issue: Recently, I’ve been working on a Unity project that involves networked gameplay using the Unity.Netcode framework. Everything was working smoothly until I added literally one line code and encountered the following error:

Error sending message: Connection ID is invalid. Likely caused by sending on stale connection 0.
0x00007ff67ff50c7a (Unity) DefaultBurstRuntimeLogCallback
0x00007ff67f62239a (Unity) BurstCompilerService_CUSTOM_RuntimeLog
0x00007ffae60157c4 (1909337906a3f61d6c23d916e250a80) Unity.Jobs.IJobExtensions.JobStruct1<Unity.Netcode.Transports.UTP.UnityTransport.SendBatchedMessagesJob>.Execute(ref Unity.Netcode.Transports.UTP.UnityTransport.SendBatchedMessagesJob data, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, ref Unity.Jobs.LowLevel.Unsafe.JobRanges ranges, int jobIndex) -> void_9d853104ef220bf2bd4244bd726cb992 from UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null (at C:/Users/ASUS/My project (3)/Library/PackageCache/com.unity.burst@1.8.9/.Runtime/unknown/unknown:0) 0x00007ffae6011016 (1909337906a3f61d6c23d916e250a80) 76b1071e4a758cb49b53b26b58023334 0x00007ff680235fc2 (Unity) ExecuteJob 0x00007ff6802373af (Unity) ForwardJobToManaged 0x00007ff680238504 (Unity) ScheduleManagedJob 0x00007ff67f763b2d (Unity) JobsUtility_CUSTOM_Schedule_Injected 0x0000017cf038e8b5 (Mono JIT Code) (wrapper managed-to-native) Unity.Jobs.LowLevel.Unsafe.JobsUtility:Schedule_Injected (Unity.Jobs.LowLevel.Unsafe.JobsUtility/JobScheduleParameters&,Unity.Jobs.JobHandle&) 0x0000017cf038e7ab (Mono JIT Code) Unity.Jobs.LowLevel.Unsafe.JobsUtility:Schedule (Unity.Jobs.LowLevel.Unsafe.JobsUtility/JobScheduleParameters&) 0x0000017cf03b47fb (Mono JIT Code) Unity.Jobs.IJobExtensions:Run<Unity.Netcode.Transports.UTP.UnityTransport/SendBatchedMessagesJob> (Unity.Netcode.Transports.UTP.UnityTransport/SendBatchedMessagesJob) 0x0000017cf03b2d6b (Mono JIT Code) Unity.Netcode.Transports.UTP.UnityTransport:SendBatchedMessages (Unity.Netcode.Transports.UTP.UnityTransport/SendTarget,Unity.Netcode.Transports.UTP.BatchedSendQueue) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.6.0/Runtime/Transports/UTP/UnityTransport.cs:793) 0x0000017bcad4cfb3 (Mono JIT Code) Unity.Netcode.Transports.UTP.UnityTransport:Update () (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.6.0/Runtime/Transports/UTP/UnityTransport.cs:904) 0x0000017c7c9fc9b8 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr) 0x00007ffadbf34b6e (mono-2.0-bdwgc) mono_jit_runtime_invoke (at C:/build/output/Unity-Technologies/mono/mono/mini/mini-runtime.c:3445) 0x00007ffadbe6d204 (mono-2.0-bdwgc) do_runtime_invoke (at C:/build/output/Unity-Technologies/mono/mono/metadata/object.c:3066) 0x00007ffadbe6d37c (mono-2.0-bdwgc) mono_runtime_invoke (at C:/build/output/Unity-Technologies/mono/mono/metadata/object.c:3113) 0x00007ff680611494 (Unity) scripting_method_invoke 0x00007ff6805ef544 (Unity) ScriptingInvocation::Invoke 0x00007ff6805d6bb4 (Unity) MonoBehaviour::CallMethodIfAvailable 0x00007ff6805d6cda (Unity) MonoBehaviour::CallUpdateMethod 0x00007ff680071c3b (Unity) BaseBehaviourManager::CommonUpdate<BehaviourManager> 0x00007ff68007944a (Unity) BehaviourManager::Update 0x00007ff6802aa7dd (Unity) InitPlayerLoopCallbacks’::`2’::UpdateScriptRunBehaviourUpdateRegistrator::Forward
0x00007ff6802896cc (Unity) ExecutePlayerLoop
0x00007ff680289840 (Unity) ExecutePlayerLoop
0x00007ff6802900c5 (Unity) PlayerLoop
0x00007ff6812593cf (Unity) PlayerLoopController::InternalUpdateScene
0x00007ff68126600d (Unity) PlayerLoopController::UpdateSceneIfNeededFromMainLoop
0x00007ff681264311 (Unity) Application::TickTimer
0x00007ff6816dd26a (Unity) MainMessageLoop
0x00007ff6816e2ad0 (Unity) WinMain
0x00007ff682ac61be (Unity) __scrt_common_main_seh
0x00007ffb5908257d (KERNEL32) BaseThreadInitThunk
0x00007ffb5a3eaa78 (ntdll) RtlUserThreadStart

Code before the error encountered :

using Unity.Netcode;
using Unity.Netcode.Components;
using UnityEngine;

public class CustomNetworkTransform : NetworkTransform
{
Vector3 LastMyPositionFromServer;
[SerializeField] NetworkObject networkObject;

void Start()
{
if(networkObject.IsOwner)
{
CanCommitToTransform = true;
}
}
// Override the OnNetworkTransformStateUpdated method to apply the manipulated position
protected override void OnNetworkTransformStateUpdated(ref NetworkTransform.NetworkTransformState oldState, ref NetworkTransform.NetworkTransformState NewState)
{ 
if (networkObject.IsOwner)
{
LastMyPositionFromServer = NewState.GetPosition();
if (Vector2.Distance(new Vector2(LastMyPositionFromServer.x, LastMyPositionFromServer.y), new Vector2(transform.position.x, transform.position.y)) > 5f && !IsServer)
{
transform.position = LastMyPositionFromServer;
}
}
}
}

Code after the error encountered :

using Unity.Netcode;
using Unity.Netcode.Components;
using UnityEngine;

public class CustomNetworkTransform : NetworkTransform
{
Vector3 LastMyPositionFromServer;
NetworkObject networkObject;

void Awake ()
{
networkObject = GetComponent<NetworkObject>();
}
void Start()
{
if(networkObject.IsOwner)
{
CanCommitToTransform = true;
}
}
// Override the OnNetworkTransformStateUpdated method to apply the manipulated position
protected override void OnNetworkTransformStateUpdated(ref NetworkTransform.NetworkTransformState oldState, ref NetworkTransform.NetworkTransformState NewState)
{ 
if (networkObject.IsOwner)
{
LastMyPositionFromServer = NewState.GetPosition();
if (Vector2.Distance(new Vector2(LastMyPositionFromServer.x, LastMyPositionFromServer.y), new Vector2(transform.position.x, transform.position.y)) > 5f && !IsServer)
{
transform.position = LastMyPositionFromServer;
}
}
}
}

More info:

  • This error is displayed so many times as if it is a debug statement in update method on the server/host side when the client joins
  • No changes were made to the project code or configuration when this error appeared.

What I’ve Tried: I’ve taken several steps to diagnose and solve the issue, including:

  • Restarting Unity.
  • Reopening the project.
  • Checking network connections for stability.
  • Reverting the code change.
  • Ensuring all software components are up to date.

Request for Help:I haven’t been able to resolve the issue, and I’m at a loss as to what might have caused it to appear seemingly out of nowhere. This is where I’m hoping to enlist your expertise.

Any guidance, tips, or advice would be greatly appreciated. I’m eager to get to the bottom of this and return to a state of working functionality.

Thank you all in advance for your help and support. Your expertise and insights are invaluable.

I Found the solution to this, but the bug still exists. The bug is, one cannot even define awake method after inheriting the class from NetworkTransform, although I do remember I read something like this before somewhere concluding this might apply to every netcode components. for example, attaching this script will work

using Unity.Netcode;
using Unity.Netcode.Components;
using UnityEngine;

public class CustomNetworkTransform : NetworkTransform
{

}

But this code will throw that error

using Unity.Netcode;
using Unity.Netcode.Components;
using UnityEngine;

public class CustomNetworkTransform : NetworkTransform
{
void Awake ()
{

} 
}

The NetworkTransform.Awake method is defined as virtual and if you don’t override and invoke the base.Awake method then it will indeed cause problems as that is where the linear interpolators are instantiated.

Try this:

        protected override void Awake()
        {
            networkObject = GetComponent<NetworkObject>();
            base.Awake();
        }

However, I have to mention that NetworkTransform derives from NetworkBehaviour which if you need access to the NetworkObject component then I would recommend just using the NetworkObject property that is already available to you.