Unity-Technologies/UnityCsReference · error · InvalidOperationException

{UnregisterRenderPickingCallback} cannot be called during pi

Error message

{UnregisterRenderPickingCallback} cannot be called during picking rendering

What it means

UnregisterRenderPickingCallback throws InvalidOperationException when called during an active picking render pass (s_InPickingRendering is true). This prevents the callback list from being modified while it is being iterated, which would corrupt the picking pass.

Source

Thrown at Editor/Mono/Handles/HandleUtility.cs:2254

            if (s_InPickingRendering)
                throw new InvalidOperationException($"{nameof(RegisterRenderPickingCallback)} cannot be called during picking rendering");

            foreach (var registered in s_RenderPickingCallbacks)
            {
                if (registered == renderPickingCallback)
                    return false;
            }

            s_RenderPickingCallbacks.Add(renderPickingCallback);
            return true;
        }

        public static bool UnregisterRenderPickingCallback(RenderPickingCallback renderPickingCallback)
        {
            if (renderPickingCallback == null)
                throw new ArgumentNullException(nameof(renderPickingCallback));
            if (s_InPickingRendering)
                throw new InvalidOperationException($"{nameof(UnregisterRenderPickingCallback)} cannot be called during picking rendering");

            for (int i = 0; i < s_RenderPickingCallbacks.Count; ++i)
            {
                var registered = s_RenderPickingCallbacks[i];
                if (registered == renderPickingCallback)
                {
                    s_RenderPickingCallbacks.RemoveAt(i);
                    return true;
                }
            }

            return false;
        }

        // Changed to Object to support VisualElements and other non-GameObject pickable objects
        // todo refactor picking code to remove all the static collections that ferry around include/exclude lists
        [AutoStaticsCleanupOnCodeReload]
        static readonly HashSet<UnityObject> s_PickingIncludeSet = new();

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Move callback unregistration to OnDisable or another lifecycle hook outside the picking render pass.
  2. If deferred unregistration is needed during rendering, use EditorApplication.delayCall.

Example fix

// before
RenderPickingResult OnRenderPicking(in RenderPickingArgs args)
{
    HandleUtility.UnregisterRenderPickingCallback(OnRenderPicking);
    ...
}
// after
RenderPickingResult OnRenderPicking(in RenderPickingArgs args)
{
    EditorApplication.delayCall += () => HandleUtility.UnregisterRenderPickingCallback(OnRenderPicking);
    ...
}
Defensive patterns

Strategy: validation

Validate before calling

// s_InPickingRendering is private and cannot be checked directly.
// Move unregistration to a lifecycle hook outside the render pass:
void OnDisable() {
    HandleUtility.UnregisterRenderPickingCallback(OnRenderPicking);
}

Try / catch

try { HandleUtility.UnregisterRenderPickingCallback(cb); }
catch (InvalidOperationException) {
    EditorApplication.delayCall += () => HandleUtility.UnregisterRenderPickingCallback(cb);
}

Prevention

When it happens

Trigger: Calling UnregisterRenderPickingCallback from inside a RenderPickingCallback, or from code that runs during the picking render loop.

Common situations: An editor tool that self-unregisters from within its own picking callback after a one-shot render, or OnSceneGui teardown code that fires during picking.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/2a1bd6d091d84384. Report an issue: GitHub.