{"record":{"id":"072cbc57285de3d4","repo":"Unity-Technologies/UnityCsReference","slug":"delegate-already-registered-for-dropdestinationid","errorCode":null,"errorMessage":"Delegate already registered for dropDestinationId:{dropDstId}","messagePattern":"Delegate already registered for dropDestinationId:(.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Editor/Mono/DragAndDrop.bindings.cs","lineNumber":91,"sourceCode":"                    AddDropHandlerV2(DefaultProjectBrowserDropHandler);\n                    AddDropHandlerV2(DefaultInspectorDropHandler);\n                    AddDropHandlerV2(DefaultHierarchyDropHandler);\n                }\n                return m_DropHandlers;\n            }\n        }\n\n        internal static void ClearDropHandlers()\n        {\n            m_DropHandlers?.Clear();\n            m_DropHandlers = null;\n        }\n\n        internal static void AddDropHandler(int dropDstId, Delegate handler)\n        {\n            if (HasHandler(dropDstId, handler))\n            {\n                throw new Exception(\"Delegate already registered for dropDestinationId:\" + dropDstId);\n            }\n\n            if (!dropHandlers.TryGetValue(dropDstId, out var handlers))\n            {\n                handlers = new List<Delegate>();\n                dropHandlers[dropDstId] = handlers;\n            }\n            handlers.Add(handler);\n        }\n\n        internal static void RemoveDropHandler(int dropDstId, Delegate handler)\n        {\n            if (dropHandlers.TryGetValue(dropDstId, out var handlers))\n            {\n                handlers.RemoveAll(dropHandler => dropHandler == handler);\n            }\n        }\n","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/DragAndDrop.bindings.cs#L73-L109","documentation":"Thrown by DragAndDrop.AddDropHandler when the exact (dropDstId, handler) pair is already registered. HasHandler checks identity equality (dropHandler == handler), so registering the same delegate instance for the same destination twice is the trigger — distinct lambdas capturing the same target are treated as different. This is a registration-correctness guard against duplicate invocations during a drop.","triggerScenarios":"Calling DragAndDrop.AddDropHandler with the same delegate instance and same dropDstId more than once without an intervening RemoveDropHandler. Commonly when a window/editor enables its handlers in OnEnable and OnEnable runs twice (e.g. after a domain reload or re-selection) without a matching OnDisable cleanup.","commonSituations":"An EditorWindow that registers a static or cached delegate in OnEnable and is re-enabled (recompile, play-mode toggle) without unregistering; a package that registers on every assembly load.","solutions":["Always pair AddDropHandler in OnEnable with RemoveDropHandler in OnDisable using the same cached delegate instance.","Cache the handler delegate in a field so the same instance is added and removed; do not pass a fresh lambda each time.","Before adding, call DragAndDrop.HasHandler(dropDstId, handler) and skip if already present."],"exampleFix":"// before\nvoid OnEnable() { DragAndDrop.AddDropHandler(kId, HandleDrop); }\nvoid OnDisable() { /* forgot to remove */ }\n// after\nstatic readonly DragAndDrop.HierarchyDropHandler s_Handler = HandleDrop;\nvoid OnEnable()  { DragAndDrop.AddDropHandler(kId, s_Handler); }\nvoid OnDisable() { DragAndDrop.RemoveDropHandler(kId, s_Handler); }","handlingStrategy":"validation","validationCode":"if (DragAndDrop.HasHandler(dropDstId, cachedHandler)) return;\nDragAndDrop.AddDropHandler(dropDstId, cachedHandler);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Store handlers in static/instance fields so the same delegate is added and removed.","Always pair OnEnable Add with OnDisable Remove.","Re-check registration state after domain reloads."],"tags":["editor","drag-and-drop","lifecycle","delegate","registration"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}