Unity-Technologies/UnityCsReference · error · ArgumentNullException
materialFolderPath
Error message
materialFolderPath
What it means
SpeedTreeImporter.SearchAndRemapMaterials(string materialFolderPath) walks a folder for Material assets and remaps them onto the importer's source materials. A null path is rejected up front with ArgumentNullException because AssetDatabase.FindAssets would otherwise throw a less specific NullReferenceException internally. The path is the search root, so null is always a programmer error, never a valid 'search everywhere' sentinel.
Source
Thrown at Editor/Mono/AssetPipeline/SpeedTreeImporter.bindings.cs:219
{
[NativeName("MaterialsShouldBeRegenerated")]
get;
}
internal extern void SetMaterialVersionToCurrent();
internal extern SourceAssetIdentifier[] sourceMaterials
{
[FreeFunction(Name = "SpeedTreeImporterBindings::GetSourceMaterials", HasExplicitThis = true)]
get;
}
public bool SearchAndRemapMaterials(string materialFolderPath)
{
bool changedMappings = false;
if (materialFolderPath == null)
throw new ArgumentNullException("materialFolderPath");
if (string.IsNullOrEmpty(materialFolderPath))
throw new ArgumentException(string.Format("Invalid material folder path: {0}.", materialFolderPath), "materialFolderPath");
var guids = AssetDatabase.FindAssets("t:Material", new string[] { materialFolderPath });
List<Tuple<string, Material>> materials = new List<Tuple<string, Material>>();
foreach (var guid in guids)
{
var path = AssetDatabase.GUIDToAssetPath(guid);
// ensure that we only load material assets, not embedded materials
var material = AssetDatabase.LoadMainAssetAtPath(path) as Material;
if (material)
materials.Add(new Tuple<string, Material>(path, material));
}
var importedMaterials = sourceMaterials;
foreach (var material in materials)
{View on GitHub (pinned to 225b0fbdb5)
Solutions
- Pass an explicit material folder path string such as "Assets/MyProject/Materials".
- If the folder is optional, branch on null before calling and skip/search a default location instead.
- Validate the path at the UI/setting layer so a null never reaches the API.
Example fix
// before
importer.SearchAndRemapMaterials(materialFolder); // materialFolder may be null
// after
if (string.IsNullOrEmpty(materialFolder))
throw new InvalidOperationException("Material folder not configured.");
importer.SearchAndRemapMaterials(materialFolder); Defensive patterns
Strategy: validation
Validate before calling
if (materialFolderPath == null)
throw new InvalidOperationException("materialFolderPath not set");
importer.SearchAndRemapMaterials(materialFolderPath); Type guard
static bool IsUsableFolderPath(string p) => p != null;
Prevention
- Bind editor fields to a non-null default folder constant.
- When deriving the folder from selection, handle the nothing-selected case explicitly.
- Surface unset configuration at the settings UI rather than at the API call.
When it happens
Trigger: Calling SearchAndRemapMaterials(null); passing a path variable that was never initialized or came from a null config field; a UI text field bound to the argument with no value entered.
Common situations: Editor tooling that drives remapping from a project setting that is unset; scripts that derive the folder from selection when nothing is selected; migration where the expected folder constant was removed.
Related errors
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/cbcb90a6f9071e02.
Report an issue: GitHub.