CoplayDev/unity-mcp · error · ValueError

{context} must have 3 or 4 components, got {len(value)}

Error message

{context} must have 3 or 4 components, got {len(value)}

What it means

Raised by _normalize_color (texture.py:140) when the value is a list/tuple whose length is neither 3 nor 4. Colors must be RGB (3) or RGBA (4) component arrays. The 'context' label names the field (e.g. 'color must have 3 or 4 components, got 2').

Source

Thrown at Server/src/cli/commands/texture.py:140

                if _is_normalized_color(color):
                    return [int(round(float(c) * 255)) for c in color]
                return [int(c) for c in color]
            except (TypeError, ValueError):
                raise ValueError(f"{context} dict values must be numeric, got {value}")
        raise ValueError(f"{context} dict must have 'r', 'g', 'b' keys, got {list(value.keys())}")

    if isinstance(value, (list, tuple)):
        if len(value) == 3:
            value = list(value) + [1.0 if _is_normalized_color(value) else 255]
        if len(value) == 4:
            try:
                if _is_normalized_color(value):
                    return [int(round(float(c) * 255)) for c in value]
                return [int(c) for c in value]
            except (TypeError, ValueError):
                raise ValueError(
                    f"{context} values must be numeric, got {value}")
        raise ValueError(
            f"{context} must have 3 or 4 components, got {len(value)}")

    raise ValueError(f"{context} must be a list or hex string")


def _normalize_palette(value: Any, context: str) -> list[list[int]]:
    if value is None:
        return []
    if isinstance(value, str):
        value = try_parse_json(value, context)
    if not isinstance(value, list):
        raise ValueError(f"{context} must be a list of colors")
    return [_normalize_color(color, f"{context} item") for color in value]


def _normalize_pixels(value: Any, width: int, height: int, context: str) -> list[list[int]] | str:
    if value is None:
        raise ValueError(f"{context} is required")

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Provide exactly 3 ([r,g,b]) or 4 ([r,g,b,a]) numeric components.
  2. If you omitted alpha, switch to the 3-element form (alpha defaults to 255 or 1.0).
  3. Double-check the array length before passing it as a color.

Example fix

# before
unity-mcp texture create Assets/Tex.png --color '[255,0]'

# after
unity-mcp texture create Assets/Tex.png --color '[255,0,0]'
Defensive patterns

Strategy: validation

Validate before calling

# Python - check length before _normalize_color
if isinstance(value, (list, tuple)) and len(value) not in (3, 4):
    raise ValueError(f"{context} must have 3 or 4 components, got {len(value)}")

Type guard

def is_valid_color_length(v) -> bool: return isinstance(v,(list,tuple)) and len(v) in (3,4)

Try / catch

try:
    color = _normalize_color(value, context)
except ValueError as e:
    print_error(str(e)); sys.exit(1)

Prevention

When it happens

Trigger: Passing --color '[255,0]' (2 elements), '[255,0,0,0,0]' (5), or a single-element list. Applies to --color, palette items, pixel entries, and set-pixels color when given as a list.

Common situations: Typing an incomplete color (forgot a channel); passing a 2D point or pivot array into a color argument; a palette generator emitting pairs instead of triples.

Related errors


AI-assisted analysis of CoplayDev/unity-mcp@c21bf496bc (2026-08-13). Data as JSON: /api/errors/633db601309411b2. Report an issue: GitHub.