- Home /
Is There Any Way To Save A Variable Into a .txt file?
Ok so is there any way by which i can save a variable, such as score, points, etc. in a .txt file? which i can load later in the game...? i know we can use playerprefs.SetInt() and PlayerPrefs.GetInt() to save a variable very easily but i am curious if we could save it into a .txt or any other file format? using the unity I/O?
let the variable be...
var MyScore : int = 10;
Answer by HarshadK · Oct 21, 2014 at 01:48 PM
Watch this video: PERSISTENCE - SAVING AND LOADING DATA
It nicely describes many aspects of how to read and write to files.
Also you can use any file extension as you like let it be .txt, or .dat, or .blablablabla.
Specifically for text file read this: How do I write a line of text to a file?
Now you can do research further on how to read similarly.
yes, @harshadk thanks again i found this somewhere...
System.IO.File.WriteAllText("D:/Save/posX.txt", posx.ToString());
i used it and it worked...
similarly i tried replaciing Wri$$anonymous$$llText with ReadAllText and astonishingly i was able to read the data also...
I wanted to create my own save game system rather any built in or ready made system..
You could also look into saving to a X$$anonymous$$L file if you want some more structure. Useful if your save game data is mroe than a simple variable.
right now i need only to save 4 simple variables, namely players coordiantes, health, resource and score...
i will also see to X$$anonymous$$L file saving system for unity
Your answer
Follow this Question
Related Questions
How to make a editable text list using external text file 1 Answer
How to use corutines in a class 1 Answer
JS to C# class conversion issue. 1 Answer
Declare new class[].vector3[] length? 2 Answers