CoplayDev/unity-mcp · error · ArgumentException

Width should be larger than 0

Error message

Width should be larger than 0

What it means

Thrown by SceneScreenshotterBase.ValidateSettings when Settings.Width is zero or negative. Width is used to dimension the output screenshot texture. The validation runs synchronously inside Screenshot() before any camera rendering.

Source

Thrown at TestProjects/AssetStoreUploads/Packages/com.unity.asset-store-tools/Editor/Previews/Scripts/Generators/Custom/Screenshotters/SceneScreenshotterBase.cs:39

        private Camera GetCamera()
        {
            if (_camera == null)
            {
#if UNITY_2022_3_OR_NEWER
                _camera = GameObject.FindFirstObjectByType<Camera>(FindObjectsInactive.Include);
#else
                _camera = GameObject.FindObjectOfType<Camera>();
#endif
            }

            return _camera;
        }

        public virtual void ValidateSettings()
        {
            if (Settings.Width <= 0)
                throw new ArgumentException("Width should be larger than 0");

            if (Settings.Height <= 0)
                throw new ArgumentException("Height should be larger than 0");

            if (Settings.Depth <= 0)
                throw new ArgumentException("Depth should be larger than 0");

            if (Settings.NativeWidth <= 0)
                throw new ArgumentException("Native width should be larger than 0");

            if (Settings.NativeHeight <= 0)
                throw new ArgumentException("Native height should be larger than 0");
        }

        public abstract void PositionCamera(GameObject target);

        public string Screenshot(string outputPath)
        {

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Set Settings.Width to a positive integer (e.g., 1024 or 1920) before calling Screenshot.
  2. If settings come from a serialized source, apply sensible defaults after deserialization.
  3. Add a UI-side clamp so width cannot be saved as zero.

Example fix

// before
var settings = new SceneScreenshotterSettings { Width = 0, Height = 1080, ... };
screenshotter.Screenshot(outputPath);

// after
var settings = new SceneScreenshotterSettings { Width = 1920, Height = 1080, ... };
screenshotter.Screenshot(outputPath);
Defensive patterns

Strategy: validation

Validate before calling

if (settings.Width <= 0)
    settings.Width = 1920; // apply default
// or validate before calling Screenshot:
if (settings.Width <= 0)
    throw new InvalidOperationException("Screenshot Width must be set to a positive value.");

Type guard

static bool IsValidScreenshotDimensions(SceneScreenshotterSettings s)
    => s.Width > 0 && s.Height > 0 && s.Depth > 0 && s.NativeWidth > 0 && s.NativeHeight > 0;

Try / catch

try
{
    screenshotter.Screenshot(outputPath);
}
catch (ArgumentException ex) when (ex.Message.Contains("Width should be larger than 0"))
{
    settings.Width = 1920;
    screenshotter.Screenshot(outputPath); // retry with default
}

Prevention

When it happens

Trigger: Calling Screenshot(outputPath) or ValidateSettings() on a SceneScreenshotterBase subclass with Settings.Width <= 0.

Common situations: Screenshot settings deserialized from config with a missing/default width field; UI slider left at 0; settings object constructed without setting Width after a reset.

Related errors


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