x


Problem Reading A Text File

Hi,

I have a script which reads the contents of a text file and then outputs to the GUI. It works fine in the editor, in scene and game view, but when I build and run (either the web player or a Mac standalone) I'm not seeing the text. Am I overlooking anything?

This is the script. The file is just a plain text file and sits in the Resources folder in my Assets folder.

import http://System.IO;
var fileName = "ratfacts.txt";
var letterPause = 0.2;
var linePause = 10;
var sound : AudioClip;
private var word;

function Start () {
    var sr = new StreamReader(Application.dataPath + "/Resources/" + fileName);
    var fileContents = sr.ReadToEnd();
    sr.Close();
    var lines = fileContents.Split("\n"[0]);
    for (line in lines) {
        word = line;
//      guiText.text += line + "\n";

        TypeText();
        if(word.length > 1){
            yield WaitForSeconds(linePause);
        }else{
            yield WaitForSeconds(2);
        }
    }
}

function TypeText () {
    for (var letter in word.ToCharArray()) {
        guiText.text += letter;
        if (sound)
            audio.PlayOneShot (sound);
        yield WaitForSeconds (letterPause);
    }
    guiText.text += "\n";
}
more ▼

asked Apr 07 '11 at 08:54 AM

Drum gravatar image

Drum
61 7 10 14

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

The StreamReader constructor that takes a string is expecting a filename. In a webplayer, Application.dataPath will return a URL instead of a filename.

To read text from a URL in Unity, use the WWW class.

Your code can detect if its running in the webplayer or not by checking Application.isWebPlayer. That way you can have it pick which way it needs to read the file.

Edit: Resources.Load loads assets like textures and audio, regardless of whether you are in the webplayer or not. I'm guessing you used that to load textures and audio. It should also be possible to read in your text file that way. The object it returns will be of type TextAsset.

more ▼

answered Apr 07 '11 at 05:05 PM

Bampf gravatar image

Bampf
5k 8 19 49

Ah! I understand now. Thanks Bampf.

For some reason accessing this site from this computer doesn't recognise me as the same drum who asked the question, so I can't mark it as correct. I will give both you guys a vote up and mark correct tomorrow when I'm back in the office.

Thanks again.

Apr 07 '11 at 06:26 PM Drum
(comments are locked)
10|3000 characters needed characters left

There is no physical Resources folder in a build. Also, a webplayer can't load external files from disk anyway (only through the web). Sounds like you should just use a TextAsset rather than loading stuff, since you don't seem to need an actual external file.

more ▼

answered Apr 07 '11 at 09:14 AM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

I see. I'll certainly look at TextAsset, but I think I'm confused because I have a lot of audio and some imagery in the project which is in the Resources folder and is called in by script in the same way and both work fine on web-player or stand-alone builds. I was assuming text would be much the same.

Apr 07 '11 at 10:38 AM Drum
(comments are locked)
10|3000 characters needed characters left

I think the problem is fileContents.Split("n"[0]); as the split fuction will not work on webplayer. I've got that prob at the minute but still have no answer to it.

more ▼

answered Nov 27 '11 at 08:01 PM

phil gravatar image

phil
1

(comments are locked)
10|3000 characters needed characters left

I think the problem is this line: var lines = fileContents.Split("n"[0]);

The .Split is from the .NET functions with (MS windows).

more ▼

answered Nov 27 '11 at 08:01 PM

phil gravatar image

phil
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3692
x553
x313
x242
x78

asked: Apr 07 '11 at 08:54 AM

Seen: 2961 times

Last Updated: Nov 27 '11 at 08:01 PM