Resources.Load is not working with an existing file in Resources?

I am attempting to load a created ScriptableObject. The file gets placed correctly, and exists in a folder under /Resources/, as it is supposed to.

However, when it is actually loaded via Resources.Load, it returns a Null - even though a System.IO.File.Exists says that the path is valid - and that the file is actually correct. This is what handles the file load. All of the correct variables show up in Inspector and looking at the file in a text editor:

public void loadLevel(string fileName)
{
    // Path - Valid: Assets/GameAssets/Resources/Levels 
    string filePath = fileDirectory + fileName;

    if (System.IO.File.Exists(filePath + ".asset")) // The file does exist, and has content;
    {
        // Scriptable Object //
        Level tempLvl = Resources.Load<Level>(filePath); // No ".asset" on actual name 
        // Returns Null!
    }
}

Can anyone tell me what might be going on here? I did one final test - moving everything directly into Resources, but it returned the same result.

Here is a example of a file that I’m attempting to load - so it’s not actually null:

alt text

2 Answers

2

When using Resources.Load you don’t specify the full path, you specify the path within the Resources folder. So your path should be “Levels + fileName” not “Assets/GameAssets/Resources/Levels + fileName”. Also, you need to omit the file extension of .asset. Read more here: Unity - Scripting API: Resources.Load

Hah, that was it! Although it was in the comments that I only added .asset for the file check, but the directory was the problem. Thank you for that note.

Once I had the problem with Resources.Load in Unity 2019.4: existing prefabs could not be instantiated anymore (after I did a complete reimport of all assets).
I solved this by renaming the “Resources” folder to something else - then renamed it back to “Resources”. Suddenly everything worked as expected again. Hope this might help someone having the same troubles :wink:

Burn0815, thank you for sharing this! I was seeing all of my Resources in game in the editor, but not in my build. After reading your post, I did a combination of renaming the "Assets/Resources" folder to "Assets/ResourcesA" and then renaming it back to "Assets/Resources" and deleting the .meta file for the folder. One of both of those things together made the Resources appear in game. Thanks again! Have a great one.