- Home /
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);
}
Debug.Log("DONE");
}
}
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.
EDIT
I tried to move this to C# and it still says CreateText is not part of System.IO.File.
i want one clarification do u want to create .txt file or anything else
File.CreateText
is a C# command -- but maybe not in javaScript?
Also, your code only allows one save? (if they already have a save file, prints "Exists!" and quits.) A typical save won't even check if the file is there -- File.Create
automatically ceates or overwrites, which is usually what you want.
I am just wanting it to create a text file for now. But its showing the auto complete for CreateText, but its saying its not part of System.IO.File. I know this is possible, i've seen many other people do it. I have no idea why its not working.
@Owen Reynolds: File.CreateText is a .NET function, not a C# command. It's nothing to do with languages.
Answer by lenyeto · Jul 27, 2012 at 07:07 AM
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.
Answer by diddykonga · Jul 27, 2012 at 12:55 AM
You could possibly have a corrupt installion of .Net, maybe try reinstalling it? http://www.microsoft.com/en-us/download/details.aspx?id=17851
If this still doenst work, then can you post exactly what you put for your imports
Unity doesn't actually use .NET, it uses $$anonymous$$ono, and has no external dependencies.
Ah right, but i doubt mono could be missing such an important part of the System namespace, so i assume it has to be his code 0-o
Unless the subset doesnt include part of the File classes methods lol
Answer by $$anonymous$$ · Jul 25, 2012 at 03:40 PM
Would you like to use an xml file?
In unitynoobs blog you find a good way to do this.
The Xml File:
<transforms>
<rotation>
<x>values</x>
<y>values</y>
<z>values</z>
</rotation>
</transforms>
The function:
public void WriteToXml()
{
string filepath = Application.dataPath + @"/Data/gamexmldata.xml";
XmlDocument xmlDoc = new XmlDocument();
if(File.Exists (filepath))
{
xmlDoc.Load(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!
Answer by Owen-Reynolds · Jul 26, 2012 at 03:33 PM
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");
SSS.Close();
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613074652im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Loading data from a txt file - C# 3 Answers
from bool array to binary file? 1 Answer
Loading text into Gui.Box 2 Answers
How do I load a 60mb text file? 0 Answers
Checkpoint autosave Method? 0 Answers