{"record":{"id":"52c7b81d610cb1b6","repo":"CoplayDev/unity-mcp","slug":"could-not-generate-a-unique-screenshot-filename-fo","errorCode":null,"errorMessage":"Could not generate a unique screenshot filename for '{fullPath}'.","messagePattern":"Could not generate a unique screenshot filename for '(.+?)'\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"warning","filePath":"MCPForUnity/Editor/Helpers/EditorWindowScreenshotUtility.cs","lineNumber":416,"sourceCode":"        }\n\n        private static string EnsureUnique(string fullPath)\n        {\n            if (!File.Exists(fullPath))\n                return fullPath;\n\n            string directory = Path.GetDirectoryName(fullPath) ?? string.Empty;\n            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fullPath);\n            string extension = Path.GetExtension(fullPath);\n\n            for (int i = 1; i < 10000; i++)\n            {\n                string candidate = Path.Combine(directory, $\"{fileNameWithoutExtension}-{i}{extension}\");\n                if (!File.Exists(candidate))\n                    return candidate;\n            }\n\n            throw new IOException($\"Could not generate a unique screenshot filename for '{fullPath}'.\");\n        }\n\n        private static void DestroyTexture(Texture2D texture)\n        {\n            if (texture == null)\n                return;\n\n            UnityEngine.Object.DestroyImmediate(texture);\n        }\n    }\n}\n","sourceCodeStart":398,"sourceCodeEnd":428,"githubUrl":"https://github.com/CoplayDev/unity-mcp/blob/c21bf496bca87d54e75bad048563c3adb1782081/MCPForUnity/Editor/Helpers/EditorWindowScreenshotUtility.cs#L398-L428","documentation":"After capturing a screenshot, the utility searches for a non-existing filename by appending '-1' through '-9999' to the base name in the target directory. If all 9999 candidates already exist on disk, it gives up and throws IOException. This bounds disk usage from repeated captures to the same path.","triggerScenarios":"Calling the screenshot capture API thousands of times to the same directory with the same base filename, or pointing the output path at a folder already holding name-1.png through name-9999.png. The loop iterates 'for (int i = 1; i < 10000; i++)' and returns the first non-existent candidate; reaching 10000 means every one existed.","commonSituations":"Automated/looping screenshot capture with no cleanup; test suites that hammer the same path; a stale output directory that was never cleared; long-running editor sessions accumulating screenshots.","solutions":["Use a unique base filename or output directory per capture (e.g. embed a timestamp or GUID in the base name).","Clear or rotate old screenshots from the target directory before capturing.","Point the screenshot output path at a fresh/empty folder."],"exampleFix":"// before\ncaptureScreenshot(basePath: \"Assets/Shots/scene.png\"); // fixed name, collides after many runs\n\n// after\nstring stamp = DateTime.Now.ToString(\"yyyyMMdd_HHmmss_fff\");\ncaptureScreenshot(basePath: $\"Assets/Shots/scene_{stamp}.png\");","handlingStrategy":"validation","validationCode":"// Before capturing, ensure the target dir has headroom for new candidates.\nstring dir = Path.GetDirectoryName(fullPath);\nstring baseName = Path.GetFileNameWithoutExtension(fullPath);\nstring ext = Path.GetExtension(fullPath);\nint used = Directory.Exists(dir)\n    ? Directory.GetFiles(dir, $\"{baseName}-*{ext}\").Length\n    : 0;\nif (used >= 9999)\n    throw new InvalidOperationException($\"Screenshot slot exhausted in {dir}; clean up or use a unique name.\");","typeGuard":null,"tryCatchPattern":"try { return CaptureScreenshot(path); }\ncatch (IOException ex) when (ex.Message.Contains(\"unique screenshot filename\"))\n{\n    // Retry once with a timestamped base name in a fresh directory.\n    string stamp = DateTime.Now.ToString(\"yyyyMMdd_HHmmss_fff\");\n    string alt = Path.Combine(Path.GetDirectoryName(path), $\"shot_{stamp}{Path.GetExtension(path)}\");\n    return CaptureScreenshot(alt);\n}","preventionTips":["Always embed a timestamp or GUID in the screenshot base name.","Periodically clean or rotate the screenshot output directory.","Point captures at a dedicated, empty folder per session."],"tags":["screenshot","filesystem","unity-editor","io"],"backgroundTag":null,"analyzedSha":"c21bf496bca87d54e75bad048563c3adb1782081","analyzedAt":"2026-08-13T17:36:56.095Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}