Unity-Technologies/UnityCsReference · error · InvalidOperationException
You shouldn't call Initialize for Editors
Error message
You shouldn't call Initialize for Editors
What it means
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.
Source
Thrown at Editor/Mono/Inspector/Editor.cs:1510
{
foreach (UnityObject target in targets)
{
if (EditorUtility.IsPersistent(target) && !IsAppropriateFileOpenForEdit(target))
return false;
}
return true;
}
public virtual bool UseDefaultMargins()
{
return true;
}
[EditorBrowsable(EditorBrowsableState.Never)]
public void Initialize(UnityObject[] targets)
{
throw new InvalidOperationException("You shouldn't call Initialize for Editors");
}
[EditorBrowsable(EditorBrowsableState.Never)]
public void Cleanup()
{
throw new InvalidOperationException("You shouldn't call Cleanup for Editors. Dispose resources in " +
"Editor.OnDisable");
}
public bool MoveNextTarget()
{
referenceTargetIndex++;
return referenceTargetIndex < targets.Length;
}
public void ResetTarget()
{
referenceTargetIndex = 0;View on GitHub (pinned to 225b0fbdb5)
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.
Example fix
// before editor.Initialize(targets); // InvalidOperationException // after // let CreateEditor initialize it; do setup in OnEnable
Defensive patterns
Strategy: validation
Validate before calling
// Initialize is intentionally non-functional; do not call it. // Editors are created initialized via: var editor = Editor.CreateEditor(target);
Prevention
- Never call Initialize on Editor instances; rely on CreateEditor.
- Exclude Initialize from convention-based method invokers/DI containers.
- Put setup in OnEnable.
When it happens
Trigger: Explicitly invoking editor.Initialize(targets) from user code, or invoking it via reflection/serialization frameworks that auto-call public methods.
Common situations: 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.
Related errors
- You shouldn't call Cleanup for Editors. Dispose resources in
- gameObject
- Delegate already registered for dropDestinationId:{dropDstId
- You can't set the target on an editor.
- Value for parameter atlases is null
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/56ec30ca16d39956.
Report an issue: GitHub.