Unity-Technologies/UnityCsReference · error · ArgumentException

A command with id {id} already exists

Error message

A command with id {id} already exists

What it means

ArgumentException thrown by CommandService.RegisterCommand when a command with the given id already exists in s_Commands. The service forbids duplicate registrations to prevent handler ambiguity; both attribute-scanned (managed) and runtime-registered commands share the namespace.

Source

Thrown at Editor/Mono/Commands/CommandService.cs:114

        static void Initialize()
        {
#pragma warning disable UA2001 // The Banned API Analyzer produces compile errors for any new Linq code. This pre-existing usage has been suppressed, but should be rewritten if possible.
            s_Commands = ScanAttributes().ToDictionary(c => c.id, c => c);
#pragma warning restore UA2001
        }

        public static string GetCommandLabel(string commandId)
        {
            if (!Exists(commandId))
                throw new ArgumentException($"No command with id {commandId} exists", nameof(commandId));

            return s_Commands[commandId].label;
        }

        public static void RegisterCommand(string id, string label, CommandHandler handler, CommandHint hint = CommandHint.Any)
        {
            if (Exists(id))
                throw new ArgumentException($"A command with id {id} already exists", nameof(id));

            s_Commands[id] = new Command {id = id, label = label ?? id, hint = hint, handler = handler, managed = false};
        }

        public static void RegisterCommand(string id, CommandHandler handler, CommandHint hint = CommandHint.Any)
        {
            RegisterCommand(id, id, handler, hint);
        }

        public static bool UnregisterCommand(string id)
        {
            if (!Exists(id))
                return false;

            if (s_Commands[id].managed)
                Debug.LogWarning($"The command {id} was not explicitly registered by the user and should not be unregistered.");

            return s_Commands.Remove(id);

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Call UnregisterCommand(id) before registering, or guard with Exists(id).
  2. Register commands once (idempotently) using Exists(id) checks.
  3. Namespace command ids by package/author to avoid collisions with attribute-scanned commands.

Example fix

// before
CommandService.RegisterCommand("my.cmd", handler);

// after
if (!CommandService.Exists("my.cmd"))
    CommandService.RegisterCommand("my.cmd", handler);
Defensive patterns

Strategy: validation

Validate before calling

if (CommandService.Exists(id)) return;
CommandService.RegisterCommand(id, label, handler, hint);

Prevention

When it happens

Trigger: Calling RegisterCommand(id, ...) with an id that already exists — either declared via a Command attribute on a method, or previously registered at runtime. Common when a package/editor extension re-registers on domain reload without unregistering first.

Common situations: InitializeOnLoad or OnCodeLoaded code that registers commands every reload; two packages claiming the same command id; re-running a registration routine after a hot reload.

Related errors


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