Component and execution attributes
↑At a glance
Section titled “At a glance”Class-level attributes express component dependencies, edit-time behavior and execution preferences. They improve authoring workflows, but should not replace explicit runtime checks.
Quick reference
Section titled “Quick reference”| Attribute | What it does | Applies to and requires |
|---|---|---|
[AddComponentMenu("…")] |
Choose the path where a component appears in Add Component. | Class · UnityEngine |
[DisallowMultipleComponent] |
Prevent more than one compatible instance on the same GameObject. | Class · UnityEngine |
[RequireComponent(typeof(X))] |
Ensure a required component is added when attaching this script. | Class · UnityEngine |
[ExecuteInEditMode] |
Run a MonoBehaviour in Edit mode using the older mechanism. | Class · UnityEngine · Legacy |
[ExecuteAlways] |
Allow MonoBehaviour callbacks in Edit mode and Prefab Mode. | Class · UnityEngine |
[SelectionBase] |
Choose the parent object when clicking a child in Scene view. | Class · UnityEngine |
[HelpURL("url")] |
Attach a documentation link to the component help icon. | Class · UnityEngine |
[DefaultExecutionOrder(n)] |
Set relative default execution order for a MonoBehaviour class. | Class · UnityEngine |
[AssemblyIsEditorAssembly] |
Mark an entire assembly as Editor-only. | Assembly · UnityEngine |
[MovedFrom(...)] |
Help Unity remap serialized references after moving a type or namespace. | Type · UnityEngine.Scripting.APIUpdating |
How to choose
Section titled “How to choose”[AddComponentMenu("…")]
Section titled “[AddComponentMenu("…")]”Choose the path where a component appears in Add Component.
Use it when: Group your custom components under an easy-to-find gameplay menu.
Be aware: It changes discoverability, not component behavior.
Reference: Unity documentation
[DisallowMultipleComponent]
Section titled “[DisallowMultipleComponent]”Prevent more than one compatible instance on the same GameObject.
Use it when: Mark a scene controller that should exist only once per GameObject.
Be aware: It works at component attachment time; verify inheritance behavior for base components.
Reference: Unity documentation
[RequireComponent(typeof(X))]
Section titled “[RequireComponent(typeof(X))]”Ensure a required component is added when attaching this script.
Use it when: Ensure your controller has the Collider or Rigidbody it depends upon.
Be aware: Does not continuously repair an already-existing component missing a dependency.
Reference: Unity documentation
[ExecuteInEditMode]
Section titled “[ExecuteInEditMode]”Run a MonoBehaviour in Edit mode using the older mechanism.
Use it when: Understand or maintain legacy editor scripts while planning migration.
Be aware: Unity recommends ExecuteAlways for Prefab Mode-compatible workflows.
Reference: Unity documentation
[ExecuteAlways]
Section titled “[ExecuteAlways]”Allow MonoBehaviour callbacks in Edit mode and Prefab Mode.
Use it when: Update safe editor previews for objects and prefabs without entering Play mode.
Be aware: Callbacks behave differently outside Play mode. Guard changes to scenes and prefabs carefully.
Reference: Unity documentation
[SelectionBase]
Section titled “[SelectionBase]”Choose the parent object when clicking a child in Scene view.
Use it when: Make a character or machine easier to select as a whole in Scene view.
Be aware: This affects editor selection, not runtime input or hierarchy relationships.
Reference: Unity documentation
[HelpURL("url")]
Section titled “[HelpURL("url")]”Attach a documentation link to the component help icon.
Use it when: Link a complicated component to its maintenance guide.
Be aware: Link to a stable HTTPS page documenting this component.
Reference: Unity documentation
[DefaultExecutionOrder(n)]
Section titled “[DefaultExecutionOrder(n)]”Set relative default execution order for a MonoBehaviour class.
Use it when: Set a documented order between two lifecycle systems when unavoidable.
Be aware: Prefer explicit dependencies; script execution order should not become a hidden coupling mechanism.
Reference: Unity documentation
[AssemblyIsEditorAssembly]
Section titled “[AssemblyIsEditorAssembly]”Mark an entire assembly as Editor-only.
Use it when: Maintain a manually configured Editor assembly; prefer asmdefs for new projects.
Be aware: In typical Unity projects, an Editor-only asmdef is usually easier to maintain.
Reference: Unity documentation
[MovedFrom(...)]
Section titled “[MovedFrom(...)]”Help Unity remap serialized references after moving a type or namespace.
Use it when: Move a serializable class or namespace while helping Unity migrate references.
Be aware: Test migration with real serialized assets and source metadata.
Reference: Unity documentation
Expressing dependencies
Section titled “Expressing dependencies”using UnityEngine;
[DisallowMultipleComponent][RequireComponent(typeof(SphereCollider))][AddComponentMenu("Gameplay/Pickup Sensor")]public sealed class PickupSensor : MonoBehaviour{ private SphereCollider trigger;
private void Awake() { trigger = GetComponent<SphereCollider>(); if (trigger == null) { Debug.LogError("PickupSensor requires a SphereCollider.", this); enabled = false; return; } trigger.isTrigger = true; }}RequireComponent helps when the script is first attached, and the explicit check explains the failure if a scene or prefab later becomes inconsistent. Use ExecuteAlways only if edit-time execution is actually required.
