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

  1. Do not assign target; create a new Editor via Editor.CreateEditor / CreateCachedEditor with the new target object(s).
  2. Cache and reuse via CreateCachedEditorWithContext, which swaps the previous editor when the target changes.
  3. 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

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


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