Unity-Technologies/UnityCsReference · error · FileNotFoundException
No such file or directory.
Error message
No such file or directory.
What it means
Thrown by BuildPlayerContext.AddAdditionalFileToStreamingAssets when the source path is neither an existing directory nor an existing file. The method uses NPath to check for directory existence (Files enumeration) and file existence, and throws FileNotFoundException when neither resolves.
Source
Thrown at Editor/Mono/BuildPipeline/BuildPlayerContext.cs:95
///<param name="directoryOrFile">Path representing an existing file or directory. If the path doesn't exist, this function throws a FileNotFoundException.</param>
///<param name="pathInStreamingAssets">The path within the StreamingAssets folder at which to place the additional assets. If null, the file or directory is placed directly in the StreamingAssets folder.</param>
public void AddAdditionalPathToStreamingAssets(string directoryOrFile, string pathInStreamingAssets = null)
{
NPath sourcePath = directoryOrFile;
if (sourcePath.DirectoryExists())
{
NPath targetPath = pathInStreamingAssets ?? "";
foreach (var file in sourcePath.Files(true))
AddAdditionalFileToStreamingAssets(file, targetPath.Combine(file.RelativeTo(sourcePath)));
}
else if (sourcePath.FileExists())
{
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
- Verify the source path exists with File.Exists or Directory.Exists before calling AddAdditionalFileToStreamingAssets.
- Ensure any file-generation step that feeds into this call has completed successfully.
- Log the absolute resolved path in the error to catch working-directory or variable issues.
Example fix
// before
context.AddAdditionalFileToStreamingAssets("./BuildOutput/license.dat", "license.dat");
// fails if ./BuildOutput/license.dat doesn't exist yet
// after
string source = Path.GetFullPath("./BuildOutput/license.dat");
if (!File.Exists(source))
throw new FileNotFoundException($"Pre-build artifact missing: {source}");
context.AddAdditionalFileToStreamingAssets(source, "license.dat"); Defensive patterns
Strategy: validation
Validate before calling
void AddToStreamingAssetsSafe(BuildPlayerContext ctx, string sourcePath, string targetPath)
{
string abs = Path.GetFullPath(sourcePath);
if (!File.Exists(abs) && !Directory.Exists(abs))
throw new FileNotFoundException(
$"StreamingAssets source does not exist: {abs}");
ctx.AddAdditionalFileToStreamingAssets(abs, targetPath);
} Prevention
- Resolve source paths to absolute and verify File.Exists or Directory.Exists before calling AddAdditionalFileToStreamingAssets.
- Ensure any file-generation steps complete before the streaming-assets phase.
- Log the absolute path on failure to catch working-directory issues.
When it happens
Trigger: Calling AddAdditionalFileToStreamingAssets with a sourcePath that points to a non-existent file or directory — e.g., a path from a build script that hasn't been generated yet, a path with a typo, or a path relative to the wrong working directory.
Common situations: Build scripts that add pre-generated assets (license files, config, native plugins) to StreamingAssets before the file is produced, CI environments where a preceding step failed silently, or paths constructed from variables that are empty or incorrectly resolved.
Related errors
- Unable to add '{sourceFile}' to StreamingAssets. An entry fo
- 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/ca11c0f76819eea8.
Report an issue: GitHub.