- Home /
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:
Answer by SlowCircuit · Sep 27, 2020 at 11:15 PM
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: https://docs.unity3d.com/ScriptReference/Resources.Load.html
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.
Answer by Burn0815 · Apr 11, 2021 at 08:17 AM
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 ;)
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.
Your answer
Follow this Question
Related Questions
How do I get a list of filenames in a Resources folder? 6 Answers
Resources.LoadAsync() loads only one resources in the background? 0 Answers
Multiple calls of Resources.Load for the same resource 1 Answer
Load files from resource 1 Answer
Resource.LoadAll not working in build (works in editor mode) 2 Answers