• 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
4
Question by PClabs · Jun 15, 2011 at 06:42 AM · textfileiopermission

Create Text File

I am trying to create an example of Unity creating Text files / directories. Can someone point to me the correct function I need to call to have unity create a new file called "Data" or something?

This tool is meant to take a number of GUI textfields, be saved, and then loaded for later use. so simple Text files on the local harddrive is all I need.

Code I have:

 var firstnameGUI : First Name;
 var lastnameGUI : Surname;

 function OnGUI () {
     firstnameGUI = GUI.TextField (Rect (25, 80, 150, 30), firstnameGUI);
     lastnameGUI = GUI.TextField (Rect (25, 115, 150, 30), lastnameGUI);
 
 if (GUI.Button (Rect (25, 350, 120, 30), "Save User")) {
 
     //create Username
 
     usernameGUI = firstnameGUI.Substring(0,1) + lastnameGUI.Substring(0,1);
 
     //create Folder
     if (!Directory.Exists ("C:/Data/" + usernameGUI)) {
 
         Directory.CreateDirectory ("C:/Data/" + usernameGUI);
     }
     //Create Text file.. but dont know how.. How to create the Text file to save to.
     //save to textfile
 
        File.WriteAllText(firstnameGUI, lastnameGUI);
     }
 
 }
Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by CHPedersen · Jun 15, 2011 at 07:09 AM

You're very close. :)

File.WriteAllText does take two arguments like you've supplied, but the first one is the filepath. The text you want to write is the second one. So you need something like,

 System.IO.File.WriteAllText("C:\blahblah_yourfilepath\yourtextfile.txt", firstnameGUI + ", " + lastnameGUI);

That is, assuming you want your values comma-separated in the file. You can format the string that gets written in whatever way you like.

Comment
Add comment · Show 2 · 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 PClabs · Jun 15, 2011 at 09:12 AM 1
Share

Awesome. I have come closer to enlightenment!

avatar image Taylor-Libonati · Feb 26, 2015 at 07:08 PM 0
Share

Just a heads up for anyone looking at this: The character \ is an escape character. So you will either need to use \\ or / in your file path ins$$anonymous$$d.

avatar image
2

Answer by JITENDRA-RAJ · Sep 26, 2014 at 02:02 PM

You try with this it works....

System.Text.UnicodeEncoding encod = new System.Text.UnicodeEncoding(); byte[] byteData = encod.GetBytes(Downloadchat);

             System.IO.FileStream oFileStream = null;
             oFileStream = new System.IO.FileStream("D:\\chats.txt", System.IO.FileMode.Create);
             oFileStream.Write(byteData, 0, byteData.Length);
             oFileStream.Close ();




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
-3

Answer by grimmy · Oct 05, 2011 at 08:20 AM

Doesnt work for me.? I get an error:

unexpected char: 'U'.

When I do

 System.IO.File.WriteAllText("C:\Users\Simon\Desktop\exampleSaveData.txt", stringToPost);

I have no clue why its erroring on the first letter of the first folder in the path string..?? I tried rebooting Unity but I get the same error... Am I going mad??

Comment
Add comment · Show 8 · 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 CHPedersen · Oct 05, 2011 at 09:08 AM 4
Share

If you have a question of your own, start a new Question. Unity Answers doesn't work like a forum - this isn't a thread of discussion. This is supposed to be 1 Question, followed by x answers to that question.

avatar image grimmy · Oct 05, 2011 at 09:43 AM 0
Share

Okay no worries, will do. I figured as it's so tightly related that any problems arising from the solution offered should be viewable in the same place. Cheers. BTW-the answer to my problem is that you need to make sure you are running a standalone platform to use the function described above ($$anonymous$$e was set to webplayer.)

avatar image Anxo · Feb 17, 2012 at 06:09 PM 0
Share

You should not have posted your problem as an answer, Either start a new question or post it as a comment but your problem did help me. And I agree it is nice so see the solution in the same place, just post it as a comment next time to be considerate. People get emails when you post answers on something they are watching. =)

avatar image ZDS Alpha · Nov 09, 2013 at 08:25 AM 0
Share

I think to make a file in local disk C you have need to run application as ad$$anonymous$$istrator.

avatar image Bob-The-Zealot · Jul 12, 2017 at 11:02 PM 0
Share

Post your own question as a new question, not an answer >:/

Show more comments

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

parsing csv file 2 Answers

Write file after build 1 Answer

How to create and write a text file after compile/build 0 Answers

I cant read the file i wrote, unless i reload it in VS. 1 Answer

Where to put my text files when exporting a project 2 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