Search Unity

Resources.Load can't seem to find the file.

Discussion in 'Scripting' started by LootlabGames, Nov 21, 2014.

  1. LootlabGames

    LootlabGames

    Joined:
    Nov 21, 2014
    Posts:
    343
    I have a local text file at this location "Assets\DataFiles\DataFile1.txt" and I have tried many different things to get the TextAsset to load.

    Using any of these syntax I was never able to get the file to load(null reference).

    1)TextAsset dataFile = (TextAsset)Resources.Load("DataFile1", typeof(TextAsset));
    2)TextAsset dataFile = (TextAsset)Resources.Load("DataFiles/DataFile1", typeof(TextAsset));
    3)TextAsset dataFile = (TextAsset)Resources.Load("Assets/DataFiles/DataFile1", typeof(TextAsset));

    I also tried moving the file to the Assets folder and using these:

    1)TextAsset dataFile = (TextAsset)Resources.Load("DataFile1", typeof(TextAsset));
    2)TextAsset dataFile = (TextAsset)Resources.Load("Assets/DataFile1", typeof(TextAsset));

    Nothing worked for me.

    I did get it to work by creating a public variable and attaching the file to it via inspector.
    public TextAsset dataFile;

    Any ideas why I couldn't get the "Resources.Load" to find the file?
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    gremoryyx, Bunny83, scicyb and 2 others like this.
  3. LootlabGames

    LootlabGames

    Joined:
    Nov 21, 2014
    Posts:
    343
    You seem like a nice guy, not.
    I did read it...
    Anyway yeah I tried the file in the Resources directory.
    Same thing.

    Do the world a favor and RTFM on how to coexist.
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You kidding? Erry day posts here can be solved by reading the docs or searching the forums and answers, and I make helpful posts when I can

    The following works fine for me:
    TextAsset text = (TextAsset) Resources.Load( "someTextFile", typeof( TextAsset ) );
    Note that you dont need to specify the assets path, because it's 'relative to any Resources folder inside the Assets folder of your project', in this case my file is at
    "Assets/Resources/someTextFile.txt"
     
    Last edited: Nov 21, 2014
  5. ferverence

    ferverence

    Joined:
    Jul 15, 2014
    Posts:
    57
    They can also be discussed on the forum. Sounds like if it's working for you then maybe something else in the project or environment is affecting it. Try running the script in a new project to rule it out.
     
  6. avirtualfox

    avirtualfox

    Joined:
    Jun 10, 2015
    Posts:
    1
    Hey,

    I just spent the past 30 minutes battling the forces of Resources.Load and left-over carriage-return characters, so I can partially relate to your troubles. (In my case, the issue was due to the fact that splitting a file along new-line characters didn't remove carriage-return characters present on each line, resulting in blood and violence)

    Looking over your initial post, I can only really recommend trying again to place the text file in the Resources/ folder:
    Assets/Resources/Text.txt
    (note that "Resources" must be at the root of "Assets") and calling it:
    TextAsset asset = Resources.Load<TextAsset> ("Text");
    (note that in the path, you should include neither the Resources/ part, nor the file extension)
    On a side-note, if you were to place the Text.txt file into subdirectory Sub/ inside Resources/, i.e.
    Assets/Resources/Sub/Text.txt
    then you would load it with:
    TextAsset asset = Resources.Load<TextAsset> ("Sub/Text");
     
  7. Martian-Games

    Martian-Games

    Joined:
    Jan 19, 2012
    Posts:
    44
    Another common mistake: forgetting to remove the file extension from the file name in c#:
    TextAsset gameInfoAsset = Resources.Load("GameInfo.txt") as TextAsset; //WRONG
    TextAsset gameInfoAsset = Resources.Load("GameInfo") as TextAsset; //CORRECT
     
  8. SupriyaBansal

    SupriyaBansal

    Joined:
    Sep 24, 2018
    Posts:
    1
    Guys,

    Tried each and every scenario. As @katasteel and @avirtualfox mentioned, I could not resolve this problem. Are there any specific settings i need to do for the script to load my xlsx file from Resources folder under Assets?
     
    MikeWise likes this.
  9. XSpitFire

    XSpitFire

    Joined:
    Jan 22, 2018
    Posts:
    15
    Create a Resources folder in the Assets. Create / move your file to this folder. Then you can call the load method without a path.

    For example I have a material in Assets\Resources\black.mat

    Material TeleportAreaVisible = (Material)Resources.Load("black", typeof(Material));
     
    TheFastLane likes this.
  10. MikeWise

    MikeWise

    Joined:
    Dec 27, 2017
    Posts:
    14
    I just struggled through this for an hour, the main problem was in the end my path variable had two slashes in them that I could not see (text too small I guess). I need a fairly deep directory structure to keep track of things, and that is a downside.

    However a few things that might help:
    - Make sure the text file's "meta" files has type "TextScriptImporter:" (look inside). Otherwise it won't be a TextAsset.
    - I had to edit this to set it, I think DOS line endings "\r\n" might throw it off, and I changed them and it seemed to help, but in the end it read a csv with \r\n endings ok. So maybe not...
    - I changed the DOS line endings with Notepad2 (find and replace \r\n with \n and option interpret backslash set).
    - My file had the ".csv" extension, but I had to do "Resources.Load<TextAsset>(resFullName);"` without the extension.
    - Sometimes the resources don't get compiled and you have stale content. I noticed that sometimes an older file gets read in (when it actually works).
    - I am not sure how to force the resources to be compiled. I would like to know.
     
    waltermandelbrot likes this.
  11. Sarashinai

    Sarashinai

    Joined:
    Jun 16, 2013
    Posts:
    20
    LOVE YOU!
     
  12. TheFastLane

    TheFastLane

    Joined:
    Aug 27, 2018
    Posts:
    5
    @katasteel I know this is coming up on 7 years old, but there's multiple suggestions and according to your details your specific issue should be solved by @XSpitFire's answer.

    Your asset path doesn't contain a "Resources" folder. Create this folder, place your assets in there, and try again.
     
  13. Balin-Ebergy

    Balin-Ebergy

    Joined:
    Dec 21, 2012
    Posts:
    1
    Perfect.
     
  14. unity_8pIOrcLCtd3YbA

    unity_8pIOrcLCtd3YbA

    Joined:
    Feb 22, 2023
    Posts:
    1
    Also make sure that you are not using
    UnityEngine.TextCore.Text.TextAsset
    !
    When I was using it,
    Resources.Load(some file)
    would always return Null. As soon as I removed the
    using TextAsset = UnityEngine.TextCore.Text.TextAsset;
    from the top of my script, it worked.
     
  15. unity_wnj-dMS8C_8xdw

    unity_wnj-dMS8C_8xdw

    Joined:
    May 3, 2023
    Posts:
    1
    As an aside, depending on the file's extension, you may have to make sure that the asset is being imported as a text file (or just change the file's actual extension to txt.)