Skip to content

NullReferenceException in ScheduleHealthCheck #417

@Drwuu

Description

@Drwuu

Bug Description

NullReferenceException occurs in MCPForUnityEditorWindow.ScheduleHealthCheck() when the window is closed but delayed callback still executes.

Stack Trace

NullReferenceException: Object reference not set to an instance of an object
MCPForUnity.Editor.Windows.MCPForUnityEditorWindow.<ScheduleHealthCheck>b__12_0 () (at ./Library/PackageCache/com.coplaydev.unity-mcp@81ef4be7614d/Editor/Windows/MCPForUnityEditorWindow.cs:170)

Problem

Line 170 uses await connectionSection?.VerifyBridgeConnectionAsync();

The null-conditional operator ?. returns null, but await null throws NullReferenceException.

Suggested Fix

private void ScheduleHealthCheck()
{
    EditorApplication.delayCall += async () =>
    {
        if (this == null || connectionSection == null)
        {
            return;
        }
        await connectionSection.VerifyBridgeConnectionAsync();
    };
}

Explicit null check before awaiting prevents the exception.

Environment

  • Unity Version: 6.2 (6000.2.12f1)
  • Package Version: com.coplaydev.unity-mcp@81ef4be7614d

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions