Cannot read or write a txt file after build

I’ve create a txt file with some references and all I need us to read this file or write new lines on it. Before build, all tests in editor works fine. I can read the file and write withput problems.

The file is located in the Assets folder and I´m using “Application.dataPath” to reach it. After build and run, seems that both read and write functions stops to works. My big problem is the write function, as the read I can use a Resourses.Load and the write I only know to do with I/O streams.

Can someone give me a hint?

The Assets folder isn’t available in your build. Look at the Application.dataPath documentation. In the editor it returns the Assets folder, in a build it’s different.

Unity only includes assets in a build that are “used”. If an asset is referenced from a scene object, it is included in the build, however “included” assets are not seperate files. They are available as an asset. If you have a txt file, it would be a TextAsset. TextAssets can only be read. You can’t write to them since they are packed into the assetdatabase of your build.

You can of course read and write external files.
So the easiest “fix” for your problem is: Copy the txt file in the data folder of your build

If you need a file to be available after build and you are referencing it from code (I.e in a way that Unity can’t detect) you can put it in a Resources folder which are already aware of. However the file won’t be available as it was originally, I.e it’s not a standalone file in the build. It it merged into the bundle of assets and you can access its contents in a read only way. This because it’s not meant for writing, it’s an “asset” as the name implies, not a data or log file.

If you want to write to a file then that’s not really an asset and you should simply use the normal file IO routines. You can use the Application class to find the current path if where the build is running from I.e the “installation” path). However you should also be careful with this: if the installation path is under program files then you need admin rights to write to the file. This should only be used for debugging. For production you should the user’s home dir.