Problem with creating text file with text assets

Here’s my code for creating text files

public class CreateText : MonoBehaviour {


	void Start () {

		TextAsset Tester = new TextAsset ();
		AssetDatabase.Refresh ();
		AssetDatabase.CreateAsset (Tester,"Assets/Test.txt");
		Debug.Log(AssetDatabase.GetAssetPath(Tester));
		System.IO.File.WriteAllText ("Assets/Test.txt", "My Karma");
		AssetDatabase.SaveAssets ();

	}
	


}

I’ve got a NullReferenceException everytime I run the game. What did I do wrong, plss help.

I don’t know what the actual problem in your code is, but I don’t think you’re meant to create Assets at runtime the way you’re doing.

If you need to store a file at runtime, then you should probably use the System.IO classes to write the file to Application.persistentDataPath folder. That’s guaranteed to be be writable and persistent between game sessions and even game updates.

So replace your code with this:

void Start()
{
    var filePath = System.IO.Path.Combine(Application.persistentDataPath, "Test.txt");
    System.IO.File.WriteAllText(filePath, "My Karma");
}

Haven’t tested it but you get the idea.

EDIT:
Also you should post questions like this in the Scripting subforum.

Thanks