#help please

1 messages · Page 1 of 1 (latest)

hollow swan
#
        string[] lines = System.IO.File.ReadAllLines("Resources/questions.txt");
        qline = 0;
        questionno++;
        bool whilething = false;
        while (whilething == false) {
            int t = Random.Range(0, 100);
            string test = lines[t];
            if(test==qstring) {return;};
            if (test != "True") {
                if(test != "False") {
                    qline = t;
                    whilething=true;
                }
            }
        }
``` Some of the code I have if anyone can help.
solid bronze
#

If you want a file to be added to your build, it needs to be in the /StreamingAssets/ folder

charred mesa
#

Uh, Why aren't you using Resources.Load<TextAsset>()

solid bronze
#

(or yes, you need to load it using Resources.Load if it's a type supported by unity)

#

(which text files are)

hollow swan
#

?

#

Does that support lines? I'm new to unity so I don't really know how best to do it. The only other way I can think of currently is to put all 100 lines into separate variables which seems like a waste of time.

charred mesa
#

TextAsset has a text field which contains the entire text content of the file. You can split the text using string.Split() with delimiter \n into lines

hollow swan
#

How would I put that into an array then?

charred mesa
#

A quick google search can tell you that...But,
string[] strings = Regex.Split(loadedTextAsset.text, Environment.NewLine);

hollow swan
#

ty, I'll test that

#
        string[] lines = textFile.Split(textFile.text, Environment.NewLine);```
#

is not working...

charred mesa
#

Which part? What's the error?

hollow swan
#

I'm probably being stupid, sorry.

#

There.

charred mesa
#

erm, TextAsset has no Split method

#

The code I gave you was using Regex.Split()

hollow swan
#

What should I be using?

#

Also

#

The name 'Regex' does not exist in current context

charred mesa
#

Add this at the top

using System.Text.RegularExpressions;
using System;

And this is what you should be using :

TextAsset textFile = Resources.Load<TextAsset>("Text/textFile01");
string[] lines = Regex.Split(textFile.text, Environment.NewLine);
#

Your IDE should tell you what namespace you are missing

#

Do you have it configured properly?

hollow swan
#

Should do.

#

Do I replace"textFile01" with "questions"?

charred mesa
#

Yes, if your file is named questions. Note, Resources.Load takes the path to an asset relative to its parent Resources folder.
So, if your hierarchy is like Assets/Resources/SomeFolder/questions.txt, you'll pass SomeFolder/questions into Resources.Load

hollow swan
#

ok, I'll try that. ty

#
NullReferenceException: Object reference not set to an instance of an object
Player.newQuestion () (at Assets/Player.cs:205)
Player.Start () (at Assets/Player.cs:60)
charred mesa
#

Can you screenshot where exactly you put the questions.txt file in unity

#

and your current code too

hollow swan
charred mesa
#

and your code?

hollow swan
#

I saved the old line if I needed it in the future.

charred mesa
#

you just need to pass "questions"

#

not "Text/questions"

#

Its the path to your file relative to the resources folder. You do not have any folder named Text which contains questions inside a Resources folder :P

hollow swan
#

Seems to be working within unity now. Ty.

#

I'll just test on webGL

charred mesa
#

great! 🍀

hollow swan