- Home /
Text not loading error
Hi. I've attempted to create code to list a number of question packs based on text files placed in resources. However when I run the build I get an error in the development console:
IsolatedStorageException: Could not find a part of the path "C:\Users\TCM\Desktop\2questionnaire\QuizEarlyPrototype\Assets\Resources\QuestionPacks\General.txt"
This is the scene's code.
for( int i = 0; i < texts.Length; i++) {
//Console.WriteLine (texts[i].name.ToString ());
if(GUI.Button(new Rect(Screen.width - 400, 80+(80*i), 300, 70),texts[i].name.ToString(),PackGui))
{
//Debug.Log ("Done?");
string[] lines = File.ReadAllLines (@"Assets/Resources/QuestionPacks/" + texts[i].name.ToString() + ".txt");
Application.LoadLevel (12);
}}
Any idea what might have caused this and how I would fix it? If I make an Iphone or android port would I have to use different pathways?
Ok so i figured out what was going on. The text files weren't recreated in the build files so I just copied them into the assets/resources/etc folder in the build.
Is there anyway I can get Unity to automatically output the files ins$$anonymous$$d? It seems to load the text assets just fine but using ReadAllLines doesn't seem to work within builds
What you are doing will not work for many builds. You will not have permission to copy the files to that folder. See my answer below for how you can load string across all platforms.
Answer by robertbu · May 26, 2014 at 04:12 PM
Paths like you specify in your code don't exist in a build. What you want to do is use Resources.Load() of a TextAsset. Rather than 'ReadAllLines()', you can use Split() to produce your array of strings from the TextAsset.text. Something like:
TextAsset ta = Resources.Load ("QuestionPacks/" + texts[i].name.ToString(), typeof(TextAsset)) as TextAsset;
string[] lines = ta.text.Split (new[] { '\r', '\n' }, System.StringSplitOptions.RemoveEmptyEntries);
Thanks for the reply. I tried implementing that, however I received an overload error. The second argument requires System.File.Encoding.
I attempted to replace "typeof(TextAsset)" with "encoding:TextAsset" but it doesn't seem to work. I also tried using "as TextAsset" without the second arguement, but no luck there either.
Any advice? I haven't encountered Encoding before and I'm not sure how to approach it. I looked it up and haven't been able to find much.
Sorry. Fingers getting ahead of my brain. I corrected the code in my answer.
I wouldn't have realized I needed both "type of " and "as text asset". Thank you very much! You were a great help.
There is a C# generic version that doesn't have to be cast:
TextAsset ta = Resources.Load<TextAsset>("QuestionPacks/" + texts[i].name.ToString());