Unity-Technologies/UnityCsReference · error · ArgumentException
Unable to add '{sourceFile}' to StreamingAssets. An entry fo
Error message
Unable to add '{sourceFile}' to StreamingAssets. An entry for '{targetPath}' has already been added, '{existingValue}'. What it means
Thrown by BuildPlayerContext.AddAdditionalFileToStreamingAssets when a file is added for a targetPath that already has a different source file registered. The StreamingAssetFiles dictionary maps target paths to source files; a second registration under the same target with a different source is treated as a conflict. Adding the identical file twice is silently ignored.
Source
Thrown at Editor/Mono/BuildPipeline/BuildPlayerContext.cs:108
NPath targetPath = pathInStreamingAssets ?? sourcePath.FileName;
AddAdditionalFileToStreamingAssets(sourcePath, targetPath);
}
else
{
throw new FileNotFoundException("No such file or directory.", sourcePath.ToString());
}
}
private void AddAdditionalFileToStreamingAssets(NPath sourceFile, NPath targetPath)
{
if (StreamingAssetFiles.TryGetValue(targetPath, out var existingValue))
{
// If someone is adding the same file more than once we ignore subsequent adds
if (existingValue == sourceFile)
return;
// Throw an exception and tell the user what the problem is
throw new ArgumentException(
$"Unable to add '{sourceFile}' to StreamingAssets. An entry for '{targetPath}' has already been added, '{existingValue}'.");
}
StreamingAssetFiles.Add(targetPath, sourceFile);
}
}
}
View on GitHub (pinned to 225b0fbdb5)
Solutions
- Ensure each targetPath is unique across all AddAdditionalFileToStreamingAssets calls — namespace by feature or subfolder if needed.
- If two sources legitimately need the same name, place them in different subdirectories under StreamingAssets.
- Audit all AddAdditionalFileToStreamingAssets callers (including third-party plugins) to find the conflicting registration.
Example fix
// before
context.AddAdditionalFileToStreamingAssets("config/dev.json", "settings.json");
context.AddAdditionalFileToStreamingAssets("config/prod.json", "settings.json"); // conflict!
// after
context.AddAdditionalFileToStreamingAssets("config/dev.json", "dev/settings.json");
context.AddAdditionalFileToStreamingAssets("config/prod.json", "prod/settings.json"); Defensive patterns
Strategy: validation
Validate before calling
static readonly HashSet<string> s_registeredTargets = new();
void AddToStreamingAssetsUnique(BuildPlayerContext ctx, string sourcePath, string targetPath)
{
if (s_registeredTargets.Contains(targetPath))
throw new ArgumentException(
$"Duplicate StreamingAssets target '{targetPath}'. Use a unique subfolder.");
s_registeredTargets.Add(targetPath);
ctx.AddAdditionalFileToStreamingAssets(sourcePath, targetPath);
} Prevention
- Namespace StreamingAssets target paths by feature or plugin to avoid collisions.
- Maintain a registry of registered target paths and check before adding.
- Coordinate between multiple build processors that add files to StreamingAssets.
When it happens
Trigger: Two calls to AddAdditionalFileToStreamingAssets that map different source files to the same target path inside StreamingAssets — e.g., adding 'config/local.json' and 'prod/config.json' both as 'config.json'.
Common situations: Multiple build processors or plugins each adding files to StreamingAssets without coordinating target paths, a loop that adds files with a fixed target name, or two features both wanting 'settings.json' at the StreamingAssets root.
Related errors
- No such file or directory.
- Scene path "{0}" contains invalid directory separators.
- The build target does not support build appending.
- The build cannot be appended.
- Non-development build cannot allow debugging. Either add the
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/3e75db0a408ae6f6.
Report an issue: GitHub.