- Home /
Question by
trevortackettVG · Oct 24, 2021 at 04:02 AM ·
unity 5text
Reference text files after unity build (on MacOS)
I am creating a random name generator in Unity using two text files that contain 100,000+ first names in one file and about 250,000+ surnames in another. I am trying to access those files post-build however the game cannot access them. It works great in the editor but I assume the built app can't find the correct path for the text files. If anyone has insight on how to get this to work on MacOS that would be awesome as I want to use this as a stand alone app for name generating for writing, D&D etc.
string[] firstNames;
string[] lastNames;
public Text generatedName;
void Start()
{
//Reads text files line by line and uses that info to supply names for
//the generateName function
string path = Application.dataPath + "/Resources/first_names.txt";
string path2 = Application.dataPath + "/Resources/last_names.txt";
firstNames = File.ReadAllLines(path);
Debug.Log(firstNames);
lastNames = File.ReadAllLines(path2);
//instantiate the text field to be blank upon game start
generatedName.text = "";
}
//used to generate random names from first & last name strings
public void generateName()
{
System.Random rand = new System.Random();
int index = rand.Next(firstNames.Length);
System.Random rand2 = new System.Random();
int index2 = rand.Next(lastNames.Length);
generatedName.text = firstNames[index] + " " + lastNames[index2];
}
public void QuitProgram()
{
Application.Quit();
}
Comment
Your answer
Follow this Question
Related Questions
Getting pixel font to look decent 1 Answer
UI Text VS TextMeshPro UGUI 1 Answer
Unity 5 Text: guiText.text not working. 0 Answers
Pass through control on UI elements? 0 Answers