Unity-Technologies/UnityCsReference · error · ArgumentException
No command with id {commandId} exists
Error message
No command with id {commandId} exists What it means
ArgumentException thrown by CommandService.GetCommandLabel when no command with the given commandId is registered. Commands are populated at code-load time via ScanAttributes() into s_Commands (id-keyed) and extended via RegisterCommand; GetCommandLabel indexes that dictionary, so an unknown id is a usage error.
Source
Thrown at Editor/Mono/Commands/CommandService.cs:106
}
[AutoStaticsCleanupOnCodeReload]
private static Dictionary<string, Command> s_Commands;
[NoAutoStaticsCleanup] // Fixed single-element sentinel args array; holds only a null, no user-code references, safe to persist across reload.
private static readonly object[] k_DefaultArgs = { null };
[OnCodeLoaded]
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)View on GitHub (pinned to 225b0fbdb5)
Solutions
- Call CommandService.Exists(commandId) before GetCommandLabel and handle the missing case.
- Verify the id matches the registered Command attribute/registration exactly (case-sensitive).
- Ensure the owning assembly is loaded before querying.
Example fix
// before var label = CommandService.GetCommandLabel(id); // after var label = CommandService.Exists(id) ? CommandService.GetCommandLabel(id) : null;
Defensive patterns
Strategy: validation
Validate before calling
if (!CommandService.Exists(commandId)) return null; return CommandService.GetCommandLabel(commandId);
Try / catch
try { return CommandService.GetCommandLabel(id); }
catch (ArgumentException) { return null; } Prevention
- Call Exists before GetCommandLabel.
- Match command ids exactly (case-sensitive).
- Ensure the owning assembly is loaded before querying.
When it happens
Trigger: Calling CommandService.GetCommandLabel(commandId) for an id never registered (not declared via attribute and not registered at runtime). Also when the id string casing/spelling differs from the registered one.
Common situations: Referencing a menu/shortcut command id that was removed, renamed, or lives in an assembly that did not load. Typing the id string differently from the declared attribute. Querying before OnCodeLoaded initialization completes.
Related errors
- A command with id {id} already exists
- Command {id} does not exist
- Command ({id}, {command.hint}) does not match the hinting {h
- Unexpected resultBits Span length. The expected length of re
- Not a valid handle, has it been released already?
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/95154c55117aaab0.
Report an issue: GitHub.