#help please
1 messages · Page 1 of 1 (latest)
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.
If you want a file to be added to your build, it needs to be in the /StreamingAssets/ folder
Uh, Why aren't you using Resources.Load<TextAsset>()
(or yes, you need to load it using Resources.Load if it's a type supported by unity)
(which text files are)
?
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.
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
How would I put that into an array then?
A quick google search can tell you that...But,
string[] strings = Regex.Split(loadedTextAsset.text, Environment.NewLine);
ty, I'll test that
string[] lines = textFile.Split(textFile.text, Environment.NewLine);```
is not working...
Which part? What's the error?
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?
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
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)
Can you screenshot where exactly you put the questions.txt file in unity
and your current code too
and your code?
I saved the old line if I needed it in the future.
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
great! 🍀