.txt files diappear after build is done

I use .txt files in my game - they are located in …/Resources/Text/. I load them like this

private void LoadWallOfText(string file)
	{
		_lines = File.ReadAllLines(Application.dataPath + "/Resources/Text/WallofText/" + file + ".txt");
	}

where _lines is a string array. After building the project I realized there was no text in the game, I checked output_log.txt and found out that the game can’t see the Text folder, even though other resources like music and pictures work fine.

I handled this situation by pasting the whole Text folder to my build’s Resources folder - and it works fine now. The only problem is that .txt files are now vulnerable to any changes from the users.

Is this a common issue? Is there a way to fix this?

Instead of using File.ReadAllLines for this, you could store the text file as an asset, and use Resources.Load. This will allow Unity to package the file up into the build, and still use it like a loaded file.

Alternatively, if you have a public ‘TextAsset’ variable in your class, you can even assign the file in the inspector, the same way you would a texture or prefab.

public TextAsset text;

private string _lines;

void Start()
{
    if(text)
    {
        _lines = text.text;
    }
}