Trying to learn to save. Can I please see some basic example code for saving one variable to a file with explanation?

I was following along with the unity website on persistence and loading and saving data, but its too hard to follow for me as I am 14. I just started programming around November. I need something to save basic data to files (like XML) but similar. will a text asset do this?

If you want to save a string to a file then one way I have come across is to use the UnityEditor.EditorUtility.SaveFilePanel function to get a file location (not really necessary but why not :slight_smile: in combination with System.IO.File.WriteAllText… I can’t seem to find a url in the docs for that last one, sorry.

in c++ this would look something like…

public void SaveStringToFile (string stringToSave, string folderName, string fileName, string fileExtension) 
  // Find the assets folder and append the save folder
  string assetsFolder = Application.dataPath;
  string combinedPath = assetsFolder + "/" + folderName;
  // Combine the path, filename and extention
  string saveFile = combinedPath + "/" + fileName +  "." + fileExtension;

  // Ask user to select a file location with these default settings
  string path = UnityEditor.EditorUtility.SaveFilePanel ("Save To XML", combinedPath, fileName + "." + fileExtension, fileExtension);

  // Save The String
  if (path.Length != 0)
    System.IO.File.WriteAllText (path, stringToSave);
    Debug.Log ("Saved " + path);

Note that the file popup window will only work within unity editor, but all it is being used for here is to let you change the default location, filename and extention… If you want to use it in a build then skip that part of the code and just use…

System.IO.File.WriteAllText (saveFile, stringToSave);

Should work ?.. Hope this helps, sorry if it doesn’t :slight_smile:



public class inventory_save {
/* members and methods and such here */

public void WriteXML() //static?

			// rememebr that this class will need [Serializable] tag above its signature
        inventory_save overview = new inventory_save();

       string filepath = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName + "\\inventory_save.xml";

        System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(inventory_save));

        System.IO.StreamWriter file = new System.IO.StreamWriter(
            filepath + "inventory_save.xml");

        writer.Serialize(file, overview);

        Console.WriteLine(filepath + "inventory_save.xml");


    public void ReadXML()
        string filepath = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName + "\\inventory_save.xml";

        System.Xml.Serialization.XmlSerializer reader = new System.Xml.Serialization.XmlSerializer(typeof(inventory_save));

        //System.IO.StreamReader file = new System.IO.StreamReader(@"c:	emp\elementz_save01.xml");

        System.IO.StreamReader file = new System.IO.StreamReader(filepath + "elementz_save01.xml");

        inventory_save overview = new inventory_save();
        overview = (inventory_save)reader.Deserialize(file);

        Console.WriteLine("DEBUG XML: " + filepath + "/" + "inventory_save.xml");