{"record":{"id":"56ec30ca16d39956","repo":"Unity-Technologies/UnityCsReference","slug":"you-shouldn-t-call-initialize-for-editors","errorCode":null,"errorMessage":"You shouldn't call Initialize for Editors","messagePattern":"You shouldn't call Initialize for Editors","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Inspector/Editor.cs","lineNumber":1510,"sourceCode":"        {\n            foreach (UnityObject target in targets)\n            {\n                if (EditorUtility.IsPersistent(target) && !IsAppropriateFileOpenForEdit(target))\n                    return false;\n            }\n\n            return true;\n        }\n\n        public virtual bool UseDefaultMargins()\n        {\n            return true;\n        }\n\n        [EditorBrowsable(EditorBrowsableState.Never)]\n        public void Initialize(UnityObject[] targets)\n        {\n            throw new InvalidOperationException(\"You shouldn't call Initialize for Editors\");\n        }\n\n        [EditorBrowsable(EditorBrowsableState.Never)]\n        public void Cleanup()\n        {\n            throw new InvalidOperationException(\"You shouldn't call Cleanup for Editors. Dispose resources in \" +\n                \"Editor.OnDisable\");\n        }\n\n        public bool MoveNextTarget()\n        {\n            referenceTargetIndex++;\n            return referenceTargetIndex < targets.Length;\n        }\n\n        public void ResetTarget()\n        {\n            referenceTargetIndex = 0;","sourceCodeStart":1492,"sourceCodeEnd":1528,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Inspector/Editor.cs#L1492-L1528","documentation":"Editor.Initialize is marked EditorBrowsable(Never) and its body is a hard InvalidOperationException. Editors are initialized through internal/native creation paths, not by user code, so calling Initialize directly is treated as misuse.","triggerScenarios":"Explicitly invoking editor.Initialize(targets) from user code, or invoking it via reflection/serialization frameworks that auto-call public methods.","commonSituations":"Auto-wiring or DI frameworks that discover and invoke public methods by name. Code that assumes Initialize is a setup entry point. Copy-pasting from a MonoBehaviour-style lifecycle.","solutions":["Do not call Initialize; rely on Editor.CreateEditor/CreateCachedEditor to construct and initialize editors.","If using an auto-invocation framework, exclude Editor-derived types or the Initialize method by name.","Put custom setup logic in OnEnable, which Unity calls after the editor is created."],"exampleFix":"// before\neditor.Initialize(targets); // InvalidOperationException\n\n// after\n// let CreateEditor initialize it; do setup in OnEnable","handlingStrategy":"validation","validationCode":"// Initialize is intentionally non-functional; do not call it.\n// Editors are created initialized via:\nvar editor = Editor.CreateEditor(target);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never call Initialize on Editor instances; rely on CreateEditor.","Exclude Initialize from convention-based method invokers/DI containers.","Put setup in OnEnable."],"tags":["editor","lifecycle","api-misuse","internal"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}