Reading from a text file [SOLVED]

Hi everyone,

I am new to unity and am learning to code in C#.

The 2D application i am developing in unity requires random short messages from a database to be displayed. I was wondering how i would code the script to read from a text file and extract the messages like:

msg.txt:
Hello!
Nice, looking good!
Take that!

Where each line can be in its own separate string (maybe in a string[ ]) The text file would be updated periodically so I would need to read from it.

I’ve searched the forums a bit and there are similar questions but in JS, so I attempted to modify some of it:

StreamReader f = new StreamReader(Application.dataPath/“Resources/msg.txt”);
string fileContents = f.ReadToEnd();

but that doesnt seem to work :frowning:

So could anyone help point me in the right direction? :smile:

Read the MSDN docs on System.IO.File.

–Eric

Thanks for the reply, although I found a simpler way to obtain the string from the file.

For those wondering, I used:

string f = Resources.Load (“msgDB”).ToString(); // from Resources/msgDB.txt
string[ ] msgDB = f.Replace(“\r\n”,“\n”).Replace(“\r”,“\n”).Split(“\n”[0]);

To store the strings into an array.