This is driving me crazy, but I encounter a case where Resources.Load() randomly can’t find a file that’s 100% in the path.
My code looks like this:
/*
Load a map file
*/
private static string LoadMapFile (string mapPath) {
var mapFile = Resources.Load(mapPath) as TextAsset;
if (mapFile == null) {
throw new MapFileNotFoundException(mapPath);
}
if (string.IsNullOrEmpty(mapFile.text)) {
throw new EmptyMapFileException(mapPath);
}
return mapFile.text;
}
I call this in a loop as I am loading multiple map files.
And it failed randomly with MapFileNotFoundException, I can verify the paths are right, and it doesn’t fail at a certain map file, but randomly.
(I can put the files out of resources folder and use File.ReadAllText to load them without issues, however that won’t work with Unity build as my map files are not referenced by GameObject directly.)
If, say, Resources.Load( a_path ); returns null, can you make sure that there is actually a file at a_path inside Resources folder in Unity editor? Because if you put some files inside Resources folder runtime, Resources.Load doesn’t work for these files.
I can’t remember how I solved it, but I also stopped using Resource folder, see best practice:
It always feel awkward that file extensions are ignored when loading resources, most of my map data are now either ScriptableObject fields or JSON file being referenced by ScriptableObject.