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
1
Question by zardini123 · Jan 04, 2013 at 07:08 PM · bugfilelinesreading.txt

.txt File Bug

Hi. I'm working on a game where it reads info from a file and overwrites the file whenever i need it to. For some reason (which i dont know what the reason is), blank lines keep "spawning" in the .txt file. Heres some of the code. I hope this helps:

 function Start () { 
     var sr = File.OpenText("Assets/XXXXX/Files/charactersHealth.txt");
     fileLines = sr.ReadToEnd().Split("\n"[0]).ToList();
     sr.Close(); 
 } 
 
 function OnCollisionEnter (col : Collision) {
     var sr = File.OpenText("Assets/XXXXXX/Files/charactersHealth.txt");
     fileLines = sr.ReadToEnd().Split("\n"[0]).ToList();
     sr.Close(); 
     WriteHealth();
 }
 
 function WriteHealth () {   
     
     var ar = fileLines.ToArray();
     
     if (transform.parent.name == "XXXXX") { 
     ar[0] = health.ToString();
     }
     if (transform.parent.name == "XXXX") {
     ar[1] = health.ToString();
     } 
     File.WriteAllLines("Assets/XXXXXX/Files/charactersHealth.txt", ar);
 }
 // Some personal info is blanked out with X's

Also, other scripts with kind-of the same type of code is accessing the same file as the code above is.

Thanks for your help, -GameDude

Comment
Add comment · Show 5
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 Mr.Z · Jan 04, 2013 at 07:20 PM 0
Share

what do you mean blank lines keep "spawning"?

avatar image zardini123 · Jan 05, 2013 at 04:03 AM 0
Share

well, what happens is that in the file, it starts out with two lines which has two values. Then when i test the game, every time the character collides with something, 7 blank lines appears after the two values. And it keeps happening and soon i have at least 300 unnecessary blank lines.

avatar image zardini123 · Jan 05, 2013 at 04:16 AM 0
Share

By the way, is there any way where i can delete the unnecessary blank lines from the .txt file through the script?

avatar image Mr.Z · Jan 05, 2013 at 04:33 AM 0
Share

I'm rather bussy now and its kinda late, so I'll get back to you tomorrow, but I belive if you do a null check before writing in the txt file your problem will be solved

avatar image zardini123 · Jan 05, 2013 at 01:51 PM 0
Share

Ohh. That makes alot of sense. But how can you do that?

3 Replies

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

Answer by zardini123 · Jan 06, 2013 at 12:48 AM

Ok i figured it out. I used File.WriteLine instead. Hears some of the code:

 function WriteHealth () {   
     var ar = fileLines.ToArray();
     var blankArray : String[] = [""];
     
     if (transform.parent.name == "XXXXX") { 
         ar[0] = currentHealth.ToString();
     }
     if (transform.parent.name == "XXXXX") {
         ar[1] = currentHealth.ToString();
     } 
        var sw : StreamWriter = new StreamWriter("Assets/XXXXXXX/Files/charactersHealth.txt");
 
     sw.WriteLine(ar[0]);
     sw.WriteLine(ar[1]);
 
     sw.Flush();
     sw.Close();
 }

Now there is only one blank line no matter what. One blank line isn't that bad.

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
1

Answer by Bunny83 · Jan 06, 2013 at 12:33 AM

There is no "bug" in a txt file and also not in the File class. Everything works as it should. The problem here is that you use "WriteAllLines" which usually uses windows line endings (CR + LF). Since you split your text manually on LF (`\n`) the CR (`\r`) will remain in the text and will be written to the file as additional lines.

The File class has a lot quite useful static functions like File.ReadAllLines() and File.WriteAllLines. I'm not sure why you use a File object to read the file and WriteAllLines to write the file.

So just use File.ReadAllLines() to read the file and everything should be fine.

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 Mr.Z · Jan 05, 2013 at 02:19 PM

Alright, try this:

 function Start () { 
     var sr = File.OpenText("Assets/XXXXX/Files/charactersHealth.txt");
     fileLines = sr.ReadToEnd().Split("\n"[0]).ToList();
     sr.Close(); 
 } 
 
 function OnCollisionEnter (col : Collision) {
     var sr = File.OpenText("Assets/XXXXXX/Files/charactersHealth.txt");
     fileLines = sr.ReadToEnd().Split("\n"[0]).ToList();
     sr.Close(); 
     WriteHealth();
 }
 
 function WriteHealth () {   
 
     var ar = fileLines.ToArray();

     if (transform.parent.name == "XXXXX") { 
     ar[0] = health.ToString();
     }
     if (transform.parent.name == "XXXX") {
     ar[1] = health.ToString();
     }
 if(ar[o] && ar[1]){
             File.WriteAllLines("Assets/XXXXXX/Files/charactersHealth.txt", ar);
 }
Comment
Add comment · Show 7 · 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 zardini123 · Jan 05, 2013 at 03:08 PM 0
Share

Uh at this part: catch (Exception e){ throw new Exception(e.ToString()); } What is e?

avatar image Mr.Z · Jan 05, 2013 at 04:30 PM 0
Share

A variable of type Exception

avatar image zardini123 · Jan 05, 2013 at 04:49 PM 0
Share

I get two errors when i inputted the code which are: -Unexpected token: e -Unexpected token: throw

avatar image Mr.Z · Jan 05, 2013 at 05:40 PM 0
Share

Damn just realised thats javascript, I thought C#

avatar image zardini123 · Jan 05, 2013 at 06:05 PM 0
Share

Hahaha wow. At least most of it works. Please just translate the stuff into JS. Thanks

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

Reading a file's memory size 1 Answer

"Opening file fail"?? 3 Answers

C# How do I read an external text file asynchronously in Unity? 2 Answers

Green lines are gone? 1 Answer

Lines appear between sprites from sprite sheets 7 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