{"record":{"id":"ffcd5af5db4dc531","repo":"CoplayDev/unity-mcp","slug":"screencapture-capturescreenshotastexture-returned","errorCode":null,"errorMessage":"ScreenCapture.CaptureScreenshotAsTexture returned null and no fallback camera available.","messagePattern":"ScreenCapture\\.CaptureScreenshotAsTexture returned null and no fallback camera available\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs","lineNumber":245,"sourceCode":"            try\n            {\n#if UNITY_EDITOR\n                // In play mode, inline ScreenCapture reads a backbuffer before UITK has\n                // composited; route through WaitForEndOfFrame instead.\n                tex = Application.isPlaying\n                    ? CaptureCompositedAfterFrame(result.SuperSize)\n                    : ScreenCapture.CaptureScreenshotAsTexture(result.SuperSize);\n#else\n                tex = ScreenCapture.CaptureScreenshotAsTexture(result.SuperSize);\n#endif\n                if (tex == null)\n                {\n                    // Fallback to camera-based if ScreenCapture fails\n                    var cam = FindAvailableCamera();\n                    if (cam != null)\n                        return CaptureFromCameraToProjectFolder(cam, fileName, superSize, ensureUniqueFileName,\n                            includeImage, maxResolution, folderOverride: folderOverride);\n                    throw new InvalidOperationException(\"ScreenCapture.CaptureScreenshotAsTexture returned null and no fallback camera available.\");\n                }\n\n                int width = tex.width;\n                int height = tex.height;\n\n                byte[] png = tex.EncodeToPNG();\n                File.WriteAllBytes(result.FullPath, png);\n\n                if (includeImage)\n                {\n                    int targetMax = maxResolution > 0 ? maxResolution : 640;\n                    if (width > targetMax || height > targetMax)\n                    {\n                        downscaled = DownscaleTexture(tex, targetMax);\n                        byte[] smallPng = downscaled.EncodeToPNG();\n                        imageBase64 = System.Convert.ToBase64String(smallPng);\n                        imgW = downscaled.width;\n                        imgH = downscaled.height;","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/CoplayDev/unity-mcp/blob/c21bf496bca87d54e75bad048563c3adb1782081/MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs#L227-L263","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure at least one active Camera exists in the scene before capturing.","If running in batchmode/headless, launch with rendering flags (e.g. -force-glcore or ensure a Game view is active).","Wait for at least one frame to render after scene load before calling capture (add a small delay or frame-wait).","Open the Game view window in the Editor if it is closed."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"# Before capturing, ensure a camera exists in the scene\n# Check via read_hierarchy / read_components for Camera components\ndef scene_has_camera(hierarchy, components_map):\n    for go_id, components in components_map.items():\n        if any(c.get('type') == 'Camera' for c in components):\n            return True\n    return False","typeGuard":null,"tryCatchPattern":"try\n{\n    var result = CaptureScreenshot(...);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"no fallback camera\"))\n{\n    return new ErrorResponse(\"No renderable frame or camera available. Add a Camera to the scene.\");\n}","preventionTips":["Ensure at least one active Camera exists in the scene before capturing.","In batchmode, use rendering flags (-force-glcore, ensure Game view).","Wait one frame after scene load before capturing.","Keep the Game view open in the Editor."],"tags":["screenshot","capture","camera","rendering","runtime"],"backgroundTag":null,"analyzedSha":"c21bf496bca87d54e75bad048563c3adb1782081","analyzedAt":"2026-08-13T17:36:56.095Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}