{"record":{"id":"1f92b0f22edd663a","repo":"Unity-Technologies/UnityCsReference","slug":"you-can-t-set-the-target-on-an-editor","errorCode":null,"errorMessage":"You can't set the target on an editor.","messagePattern":"You can't set the target on an editor\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Inspector/Editor.cs","lineNumber":555,"sourceCode":"            public static readonly GUIStyle postLargeHeaderBackground = \"IN BigTitle Post\";\n\n            static BaseStyles()\n            {\n                centerStyle.alignment = TextAnchor.MiddleCenter;\n            }\n        }\n\n        bool IToolModeOwner.areToolModesAvailable\n        {\n            get\n            {\n                // tool modes not available when the target is a prefab parent\n                return !EditorUtility.IsPersistent(target);\n            }\n        }\n\n        // The object being inspected.\n        public UnityObject target { get { return m_Targets[referenceTargetIndex]; } set { throw new InvalidOperationException(\"You can't set the target on an editor.\"); } }\n\n        // An array of all the object being inspected.\n        public UnityObject[] targets\n        {\n            get\n            {\n                if (!m_AllowMultiObjectAccess)\n                    Debug.LogError(\"The targets array should not be used inside OnSceneGUI or OnPreviewGUI. Use the single target property instead.\");\n                return m_Targets;\n            }\n        }\n\n        internal virtual int referenceTargetIndex\n        {\n            get { return Mathf.Clamp(m_ReferenceTargetIndex, 0, m_Targets.Length - 1); }\n            // Modulus that works for negative numbers as well\n            set { m_ReferenceTargetIndex = (Math.Abs(value * m_Targets.Length) + value) % m_Targets.Length; }\n        }","sourceCodeStart":537,"sourceCodeEnd":573,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Inspector/Editor.cs#L537-L573","documentation":"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.","triggerScenarios":"Any assignment to editor.target = someObject, e.g. attempting to repoint an existing Editor instance to inspect a different object.","commonSituations":"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.","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)."],"exampleFix":"// before\nmyEditor.target = newTarget; // InvalidOperationException\n\n// after\nEditor.CreateCachedEditor(newTarget, null, ref myEditor);","handlingStrategy":"validation","validationCode":"// target is read-only; never assign. Create a new editor for a new target instead.\n// If you need to switch targets:\nEditor.CreateCachedEditor(newTarget, null, ref editor);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["editor","inspector","read-only","target","api-misuse"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}