Unity-Technologies/UnityCsReference · error · InvalidOperationException
You can't set the target on an editor.
Error message
You can't set the target on an editor.
What it means
Editor.target has a public getter that returns m_Targets[referenceTargetIndex], but its setter is a hard InvalidOperationException — the target of an editor is fixed at creation and cannot be reassigned. The setter exists only to satisfy the property syntax and to give a clear message to anyone who tries.
Source
Thrown at Editor/Mono/Inspector/Editor.cs:555
public static readonly GUIStyle postLargeHeaderBackground = "IN BigTitle Post";
static BaseStyles()
{
centerStyle.alignment = TextAnchor.MiddleCenter;
}
}
bool IToolModeOwner.areToolModesAvailable
{
get
{
// tool modes not available when the target is a prefab parent
return !EditorUtility.IsPersistent(target);
}
}
// The object being inspected.
public UnityObject target { get { return m_Targets[referenceTargetIndex]; } set { throw new InvalidOperationException("You can't set the target on an editor."); } }
// An array of all the object being inspected.
public UnityObject[] targets
{
get
{
if (!m_AllowMultiObjectAccess)
Debug.LogError("The targets array should not be used inside OnSceneGUI or OnPreviewGUI. Use the single target property instead.");
return m_Targets;
}
}
internal virtual int referenceTargetIndex
{
get { return Mathf.Clamp(m_ReferenceTargetIndex, 0, m_Targets.Length - 1); }
// Modulus that works for negative numbers as well
set { m_ReferenceTargetIndex = (Math.Abs(value * m_Targets.Length) + value) % m_Targets.Length; }
}View on GitHub (pinned to 225b0fbdb5)
Solutions
- Do not assign target; create a new Editor via Editor.CreateEditor / CreateCachedEditor with the new target object(s).
- Cache and reuse via CreateCachedEditorWithContext, which swaps the previous editor when the target changes.
- If using reflection/generics over properties, skip the target setter (it is intentionally non-writable).
Example fix
// before myEditor.target = newTarget; // InvalidOperationException // after Editor.CreateCachedEditor(newTarget, null, ref myEditor);
Defensive patterns
Strategy: validation
Validate before calling
// target is read-only; never assign. Create a new editor for a new target instead. // If you need to switch targets: Editor.CreateCachedEditor(newTarget, null, ref editor);
Prevention
- Treat Editor.target as read-only; create a new Editor for a new target.
- Use CreateCachedEditor/CreateCachedEditorWithContext to swap editors efficiently.
- Exclude the target setter from any reflection-based property setter loop.
When it happens
Trigger: Any assignment to editor.target = someObject, e.g. attempting to repoint an existing Editor instance to inspect a different object.
Common situations: Misunderstanding the API and trying to reuse/recycle an Editor for a new target. Code ported from a pattern where inspectors are mutable. Reflection or serialization tooling that tries to set all public properties.
Related errors
- You shouldn't call Initialize for Editors
- You shouldn't call Cleanup for Editors. Dispose resources in
- Value for parameter atlases is null
- One of the elements in atlases is null. Please check your In
- Parameter asset is null
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/1f92b0f22edd663a.
Report an issue: GitHub.