Unity-Technologies/UnityCsReference · error · ArgumentException

Key must be non-null, between 1 and 127 characters, and be a

Error message

Key must be non-null, between 1 and 127 characters, and be a valid XML tag.

What it means

Thrown by EditorDialog.ThrowIfOptOutKeyIsInvalid when the optOutKey is null/empty or fails the regex ^[A-Za-z0-9._-]{1,127}$ — i.e. it must be 1–127 characters of only alphanumerics, dot, underscore, or hyphen. The restriction exists because the key is used as an XML tag (Linux prefs), a Windows registry key name, and an NSPreference path, with the 'DialogOptOut.' prefix and a Unity registry prefix added on top.

Source

Thrown at Editor/Mono/EditorDialog.cs:202

        }

        private static void ThrowIfInvalidKey(string optOutKey)
        {
            // We want to ensure that the key string is a valid XML tag.
            // More technically, we could restrict it to whichever is more restrictive of
            // XML tags (Linux), Windows Registry key names, or NSPreference dictionaries
            // (which are all valid XML tags), but this will simplify the validation for API users.
            // Our EditorPrefs are stored in the registry, with this key as the path.
            // The maximum length of a registry key is 255 characters, so we limit the key
            // to account for the "Software\Unity Technologies\Unity Editor 5.x Automated Testing\" prefix and a 12 character suffix.
            // We also prefix the key with "DialogOptOut." (13 chars) to ensure that it is unique to the dialog box API.
            // For this reason, we will limit to a safer ~ 127 character limit.

            // This regular expression ensures that the key string:
            // Contains only alphanumeric characters, periods, underscores, and hyphens.
            // Is between 1 and 127 characters long.
            if (string.IsNullOrEmpty(optOutKey) || !Regex.IsMatch(optOutKey, @"^[A-Za-z0-9._-]{1,127}$"))
                throw new ArgumentException($"Key must be non-null, between 1 and 127 characters, and be a valid XML tag.", nameof(optOutKey));
        }

        private static void ThrowIfMessageIsInvalid(string messageText)
        {
            if (string.IsNullOrWhiteSpace(messageText))
                throw new ArgumentNullException(nameof(messageText), "Dialog message text cannot be null or whitespace.");
        }

        private static void ThrowIfButtonTextIsInvalid(string buttonText, string parameterName)
        {
            const int kMaxButtonTextLength = 64; // This limit is probably not necessary, but it's good to have a limit on the button text as well.
            if (buttonText != null && buttonText.Length > kMaxButtonTextLength)
                throw new ArgumentException($"Text on buttons must be less than {kMaxButtonTextLength} characters long.", parameterName);
        }

        /// <summary>
        /// Displays a simple alert dialog box with a title, an icon, a message, and a single button.
        /// </summary>

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Use a short, stable, machine-style identifier (e.g. 'com.company.feature.confirm') as the key.
  2. Strip/replace disallowed characters and truncate to <=127 chars before passing.
  3. Validate with the same regex before calling.

Example fix

// before
EditorDialog.DisplayDialog(..., DialogOptOutDecisionType.ForThisSession, "Delete asset? (y/n)");
// after
EditorDialog.DisplayDialog(..., DialogOptOutDecisionType.ForThisSession, "com.company.deleteAssetConfirm");
Defensive patterns

Strategy: validation

Validate before calling

static readonly Regex kKeyRe = new Regex(@"^[A-Za-z0-9._-]{1,127}$");
static string SanitizeKey(string key)
{
    key = Regex.Replace(key ?? "", @"[^A-Za-z0-9._-]", "_");
    return key.Length > 127 ? key.Substring(0, 127) : key;
}

Type guard

static bool IsValidOptOutKey(string key) =>
    !string.IsNullOrEmpty(key) && Regex.IsMatch(key, @"^[A-Za-z0-9._-]{1,127}$");

Prevention

When it happens

Trigger: Calling an EditorDialog opt-out overload (the ones taking DialogOptOutDecisionType + storageKey) with a key containing spaces, slashes, non-ASCII, exceeding 127 chars, or null/empty.

Common situations: Using a human-readable phrase or a file path as the key; a key derived from a localized string with spaces/unicode; a key longer than 127 chars from concatenating identifiers.

Related errors


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