Read and Write Text File

So before I start, yes, I used Google but I couldn’t find anything…

I want to write a Text File with the Stats of the Player (Player_Score) and place them into the Text (myTextComponent) so that the Player know what Stats they have and if the Player closes the Game the Stats are saved on the Text File, and if the Player starts the Game again the Text File will be read and put into the Game again.
This is everything i have right now:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEditor;
using System.IO;

public class Test1 : MonoBehaviour {
	public Text myTextComponent;
	int Player_Score = 0;
	public void plusone(){
		Player_Score = Player_Score +1;
	void Update(){
[MenuItem("Tools/Write file")]
    static void WriteString()
        string path = "Assets/Resources/test.txt";

        //Write some text to the test.txt file
        StreamWriter writer = new StreamWriter(path, true);

[MenuItem("Tools/Read file")]
static void ReadString(){

        string path = "Assets/Resources/test.txt";

        //Read the text from directly from the test.txt file
        StreamReader reader = new StreamReader(path); 


There are several approaches for persisting data in Unity. Here is a my take:


Unity comes with a simple system to store key-value pairs cross platform. It only works for simple types like strings, bools and numbers, but it is the easiest to use as a beginner. Be aware that the system is intended to store small data bits like the players preferred volume, etc. It is not meant to persist level data or score. However, it’s fine for beginners to experiment with it to get things going. You can always switch later.


It is possible to convert classes marked with the System.Serializable attribute to a binary file by using the BinaryFormatter from the .NET framework (by Microsoft). This system is also very easy to use and can handle large amounts of data and is also very fast. The drawback is, that you might have (small) problems when going cross platform. For example, on iOS, you have to define certain compilation symbols before the serializer can work, because it relies on reflection. All of these issues can be solved with usually on or two lines of code. On Windows it will work fine out of the box. Another problem in the long run might be that you have to keep versioning your serialized structure everytime you change the data layout of your class. Meaning, if add or remove variables to/from your class, the program will be incompatible with the already saved files on disk. To fix this, you have to write special code to handle each version.

Simple/Custom Text Serialization

As shown in your example, you can simply write lines of text to a file. Maybe you want to save score values, then you could write each value in the format “LABEL, VALUE” on a single line. Then parse the file and reconstruct the information. This is usually called a CSV file (comma separated values), or generally a text file with key-value pairs of data. Similar to this (note, this might not compile and is not tested, but you can look up file operations easily):

int scoreA = 5;
int scoreB = 2;

string serializedData = 
    "ScoreA, " + scoreA.ToString() + "

" +
"ScoreB, " + scoreB.ToString() + "

// Write to disk
StreamWriter writer = new StreamWriter("MyPath.txt", true);

// Read
StreamReader reader = new StreamReader("MyPath.txt");
string lineA = reader.ReadLine();
string[] splitA = lineA.Split(',');
scoreA = int.Parse(splitA[1]);

string lineB = reader.ReadLine();
string[] splitB = lineB.Split(',');
scoreB = int.Parse(splitB[1]);

There are also CSV-Reader/Writer libraries to handle the text parsing for you.

JSON/XML etc. Text Serialization

A robust way to save data is using existing (markup) languages/tools like JSON or XML. Some of them are builtin to Unity like the JsonUtility, others like XMLWriter are part of the .NET framework and there a multiple other 3rd party libraries you can use. This type of serialization is usually relatively slow, but can handle errors and versioning well. It’s also easy to debug save files and easy to use because of the existing documentation. There are many other standardized text file formats (e.g. YAML).

I hope this inspires some web searches. :wink: