Skip to content

Component and execution attributes

↑
Unity 6 reference

Class-level attributes express component dependencies, edit-time behavior and execution preferences. They improve authoring workflows, but should not replace explicit runtime checks.

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

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

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

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

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

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

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

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

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

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

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

Unity C# | PickupSensor.cs
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.