CoplayDev/unity-mcp · error · ArgumentException

Native width should be larger than 0

Error message

Native width should be larger than 0

What it means

Thrown by SceneScreenshotterBase.ValidateSettings when Settings.NativeWidth is zero or negative. NativeWidth is the actual render resolution passed to GraphicsUtility.GetTextureFromCamera before optional downscaling to Width. Validation fires before the camera capture.

Source

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

#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)
        {
            ValidateSettings();

            var texture = GraphicsUtility.GetTextureFromCamera(Camera, Settings.NativeWidth, Settings.NativeHeight, Settings.Depth);

            if (Settings.Width < Settings.NativeWidth || Settings.Height < Settings.NativeHeight)
                texture = GraphicsUtility.ResizeTexture(texture, Settings.Width, Settings.Height);

            var extension = PreviewConvertUtility.ConvertExtension(Settings.Format);
            var writtenPath = $"{outputPath}.{extension}";

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Set Settings.NativeWidth to a positive integer matching or exceeding Width before rendering.
  2. Initialize NativeWidth and NativeHeight together with Width and Height in settings construction.
  3. Apply a post-deserialization pass that sets NativeWidth = Width when not explicitly provided.

Example fix

// before
var settings = new SceneScreenshotterSettings { Width = 1024, Height = 1024, Depth = 24, NativeWidth = 0, NativeHeight = 2048 };

// after
var settings = new SceneScreenshotterSettings { Width = 1024, Height = 1024, Depth = 24, NativeWidth = 2048, NativeHeight = 2048 };
Defensive patterns

Strategy: validation

Validate before calling

if (settings.NativeWidth <= 0)
    settings.NativeWidth = settings.Width > 0 ? settings.Width : 1920;
// or validate:
if (settings.NativeWidth <= 0)
    throw new InvalidOperationException("NativeWidth 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("Native width should be larger than 0"))
{
    settings.NativeWidth = settings.Width;
    screenshotter.Screenshot(outputPath);
}

Prevention

When it happens

Trigger: Calling Screenshot(outputPath) or ValidateSettings() with Settings.NativeWidth <= 0. NativeWidth is passed to GetTextureFromCamera(Camera, NativeWidth, NativeHeight, Depth).

Common situations: Settings deserialized with a missing NativeWidth; settings object created with only Width/Height set but native dimensions left at default 0; mismatched config where native dimensions were cleared but output dimensions weren't.

Related errors


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