CoplayDev/unity-mcp · error · InvalidOperationException

ScreenCapture.CaptureScreenshotAsTexture returned null and n

Error message

ScreenCapture.CaptureScreenshotAsTexture returned null and no fallback camera available.

What it means

Thrown when ScreenCapture.CaptureScreenshotAsTexture returns null (meaning no frame buffer was available to capture) and FindAvailableCamera finds no camera to use as a fallback. This is a runtime environment issue — capture requires an active rendered frame and typically a rendering camera.

Source

Thrown at MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs:245

            try
            {
#if UNITY_EDITOR
                // In play mode, inline ScreenCapture reads a backbuffer before UITK has
                // composited; route through WaitForEndOfFrame instead.
                tex = Application.isPlaying
                    ? CaptureCompositedAfterFrame(result.SuperSize)
                    : ScreenCapture.CaptureScreenshotAsTexture(result.SuperSize);
#else
                tex = ScreenCapture.CaptureScreenshotAsTexture(result.SuperSize);
#endif
                if (tex == null)
                {
                    // Fallback to camera-based if ScreenCapture fails
                    var cam = FindAvailableCamera();
                    if (cam != null)
                        return CaptureFromCameraToProjectFolder(cam, fileName, superSize, ensureUniqueFileName,
                            includeImage, maxResolution, folderOverride: folderOverride);
                    throw new InvalidOperationException("ScreenCapture.CaptureScreenshotAsTexture returned null and no fallback camera available.");
                }

                int width = tex.width;
                int height = tex.height;

                byte[] png = tex.EncodeToPNG();
                File.WriteAllBytes(result.FullPath, png);

                if (includeImage)
                {
                    int targetMax = maxResolution > 0 ? maxResolution : 640;
                    if (width > targetMax || height > targetMax)
                    {
                        downscaled = DownscaleTexture(tex, targetMax);
                        byte[] smallPng = downscaled.EncodeToPNG();
                        imageBase64 = System.Convert.ToBase64String(smallPng);
                        imgW = downscaled.width;
                        imgH = downscaled.height;

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Ensure at least one active Camera exists in the scene before capturing.
  2. If running in batchmode/headless, launch with rendering flags (e.g. -force-glcore or ensure a Game view is active).
  3. Wait for at least one frame to render after scene load before calling capture (add a small delay or frame-wait).
  4. Open the Game view window in the Editor if it is closed.
Defensive patterns

Strategy: fallback

Validate before calling

# Before capturing, ensure a camera exists in the scene
# Check via read_hierarchy / read_components for Camera components
def scene_has_camera(hierarchy, components_map):
    for go_id, components in components_map.items():
        if any(c.get('type') == 'Camera' for c in components):
            return True
    return False

Try / catch

try
{
    var result = CaptureScreenshot(...);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("no fallback camera"))
{
    return new ErrorResponse("No renderable frame or camera available. Add a Camera to the scene.");
}

Prevention

When it happens

Trigger: Calling a screenshot/capture tool when the Editor has no Game view rendered (headless batchmode without -force-render), during a frame where no rendering occurred, or when no Camera component exists in the scene. The ScreenCapture API returns null when there is no valid frame buffer.

Common situations: Running Unity in batchmode without rendering setup; scene has no camera; Game view is closed or minimized in the Editor; calling capture during initial load before the first frame renders.

Related errors


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