Text file not loading after building the game

The following line gives me the error output in log. It happens ONLY when i compile with the Oculus Rift plugged in, in normal conditions it compiles just fine. I read that i have to use Resources.Load but i don’t know how to use it, can someone please help? I am using C#.


IsolatedStorageException: Could not find a part of the path "C:\Users\Rares\Desktop\Assets\Scripts\Data\ChemDB.txt".
  at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0 

  at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000] in <filename unknown>:0 

  at System.IO.File.OpenRead (System.String path) [0x00000] in <filename unknown>:0 

  at System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) [0x00000] in <filename unknown>:0 

  at System.IO.StreamReader..ctor (System.String path) [0x00000] in <filename unknown>:0 

  at System.IO.File.OpenText (System.String path) [0x00000] in <filename unknown>:0 

  at System.IO.File.ReadAllLines (System.String path) [0x00000] in <filename unknown>:0 

  at ChemDB..ctor () [0x00000] in <filename unknown>:0

If you built this out to an executable, the path you put in the parameter for ReadAllLines would be relative to your executable. Thats why the exception that is thrown indicates that you have your based project on your desktop which mean you would have an output of Temp and Library folders on your desktop, amongst other things that would make it super messy.

There are two ways to solve this, but first understand that Assets/Scripts/Data/etc doesn’t exist after you build to a platform. This is particularly why you have Resources.Load since it pulls from any of the Resources folders you define in your project. This is where you might be getting confused with the Resources methods. It 100% is reliant on Resources folder existing. When unity builds and finds Resources folders, they are compiled in a special way, a way to isolate these “whatever” files that you may call to load at any given time. The structure of your project as you know it as a project no longer is in play.

A quote from the Resources documentation:

All assets that are in a folder named “Resources” anywhere in the Assets folder can be accessed via the Resources.Load functions. Multiple “Resources” folders may exist and when loading objects each will be examined.

This is instructing you the developer that you may have Resources folders throughout your assets folders that can have Resources.Load(or whichever method/function) load those assets by name.

You can also create public fields on your scripts/classes and assign files. In this particular case you can use TextAsset(Example of text file loading) to assign a variable of that type and load it. Either way, most likely the file path doesn’t exist since it’s was relative to the project in the editor, which of course worked fine… until you compiled it out.

I would use Resources class, the Resources folders and the various types to assign your assets… barring that, i would call the Resource by name and/or folder structure in the Resources folder

Resources from the manual

Another example, lets say you have in your assets folder a file/folder structure like:

When using Resources.Load you would include the images folder using forward slashes(never back slashes)

Texture text = (Texture)Resources.Load("/Images/SomeImage.png");  // or something like that.