Trouble creating a text file

I am having trouble creating a text file. This is my save code, and i do have System and System IO imported.

function Save () {
 if (File.Exists(SavePath+SaveName)) {
 Debug.Log("Already Exsists");
 else {
 //Is in Tag x y z p y r format
 var sr = File.CreateText(SavePath+SaveName);
 var List = GameObject.FindObjectOfType(GameObject);
 for (var GO : GameObject in List) {
 sr.WriteLine(GO.tag+" "+GO.transform.position.x+" "+GO.transform.position.y+" "+GO.transform.position.z+" "+GO.transform.rotation.x+" "+GO.transform.rotation.y+" "+GO.transform.rotation.z+" "+GO.transform.rotation.w);

I am getting an error that CreateText is not part of File. And every where i look, this is exactly what it tells me to do.

I tried to move this to C# and it still says CreateText is not part of System.IO.File.

Wow, i made a HUGE error, i had set it to Web Player platform instead of Standalone, and Web Player does not support this. I can’t believe i did that.

Would you like to use an xml file?

In unitynoobs blog you find a good way to do this.

The Xml File:


The function:

public void WriteToXml()
  string filepath = Application.dataPath + @"/Data/gamexmldata.xml";
  XmlDocument xmlDoc = new XmlDocument();
  if(File.Exists (filepath))
   XmlElement elmRoot = xmlDoc.DocumentElement;
    elmRoot.RemoveAll(); // remove all inside the transforms node.
    XmlElement elmNew = xmlDoc.CreateElement("rotation"); // create the rotation node.
     XmlElement rotation_X = xmlDoc.CreateElement("x"); // create the x node.
      rotation_X.InnerText = x; // apply to the node text the values of the variable.
     XmlElement rotation_Y = xmlDoc.CreateElement("y"); // create the y node.
      rotation_Y.InnerText = y; // apply to the node text the values of the variable.
     XmlElement rotation_Z = xmlDoc.CreateElement("z"); // create the z node.
      rotation_Z.InnerText = z; // apply to the node text the values of the variable.
   elmNew.AppendChild(rotation_X); // make the rotation node the parent.
   elmNew.AppendChild(rotation_Y); // make the rotation node the parent.
   elmNew.AppendChild(rotation_Z); // make the rotation node the parent.
   elmRoot.AppendChild(elmNew); // make the transform node the parent.
   xmlDoc.Save(filepath); // save file.

I have used xml in all my projects, has been very useful. I hope it will be useful for you!

I just ran a quick test, and CreateText created a file just fine. This code (in a Unity C# program) [EDIT: mangled using statement moved below):

using System.IO;

StreamWriter SSS = File.CreateText("testAAA.txt");
SSS.WriteLine("xxx yyy");

For fun, maybe create a completely fresh C# script, on some empty, and use a really simple CreateText in Start. Then, once it’s working, add more of more of your code back and see which part is breaking it.

You could possibly have a corrupt installion of .Net, maybe try reinstalling it?

If this still doenst work, then can you post exactly what you put for your imports