Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lenyeto · Jul 25, 2012 at 12:34 PM · textsaveloadfilecreate

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mohanrao164 · Jul 25, 2012 at 01:01 PM 0
Share

i want one clarification do u want to create .txt file or anything else

avatar image Owen-Reynolds · Jul 25, 2012 at 06:19 PM 0
Share

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.

avatar image lenyeto · Jul 26, 2012 at 12:06 AM 0
Share

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.

avatar image Eric5h5 · Jul 27, 2012 at 12:39 AM 0
Share

@Owen Reynolds: File.CreateText is a .NET function, not a C# command. It's nothing to do with languages.

4 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gheeler · Dec 05, 2012 at 11:00 AM 0
Share

should mark this as the answer!

avatar image
-1

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Jul 27, 2012 at 01:01 AM 0
Share

Unity doesn't actually use .NET, it uses $$anonymous$$ono, and has no external dependencies.

avatar image diddykonga · Jul 27, 2012 at 01:11 AM 0
Share

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

avatar image lenyeto · Jul 27, 2012 at 06:57 AM 0
Share

$$anonymous$$y imports are System and System.IO.

avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lenyeto · Jul 27, 2012 at 12:24 AM 0
Share

I have import System.IO in my scripts, but both say CreateText is not part of System.IO.File

avatar image Eric5h5 · Jul 27, 2012 at 12:56 AM 0
Share

What version of Unity are you using?

avatar image lenyeto · Jul 27, 2012 at 06:58 AM 0
Share

The newest.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges