• 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 /
  • Help Room /
avatar image
Question by MccoringPL · Sep 06, 2017 at 03:58 PM · unity 5saveloadstreamreadertextfile

How to read specific line with Streamreader in c#?

Okay, im creating game for android and need make savesystem Im using C# so i dont wanna scripts in JS

Here is my script for save and load.. Im tried to make load system but nothing.. Save working ;D

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.IO;
 using System.Text;
 using System.Timers;

 public class SaveLoad : MonoBehaviour {
 
     public int moneyo;
     public int hrs;
     public string date;
     int workedhrs;
     int savenumber;
 
     public void SaveButton()
     {
         savenumber = savenumber + 1;
         Save();
     }
 
     public void LoadButton()
     {
         Load();
     }
     void Save()
     {
         using (StreamWriter sw = new StreamWriter("savegame.txt"))
         {
             sw.WriteLine(""+moneyo.ToString());
             sw.WriteLine(""+workedhrs.ToString());
             sw.WriteLine("");
             sw.WriteLine("Saved at:" + System.DateTime.Now);
             Debug.Log("saved succesfuly");
             sw.Close();
         }
     }
 
     void Load()
     {
         using (TextReader rdr = File.OpenText("savegame.txt"))
         {
             string line;
             while ((line = rdr.ReadLine()) != null)
                 Debug.Log(line);
             rdr.Close();
         }
     }
 }

And this is what i have in saved file:

0

0

Saved at:9/6/2017 5:33:02 PM

I wanna get info from first and 2nd line for get info about money and worked hrs in game

Please help me fast.

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Iarus · Sep 06, 2017 at 05:10 PM

If your file format only contains these 3 lines, this should work.

  void Load()
  {
      using (TextReader rdr = File.OpenText("savegame.txt"))
      {
          int lineIndex = 0;
          string line;
          while ((line = rdr.ReadLine()) != null)
          {
              if (lineIndex == 0)
                 moneyo = Int.Parse(line);
              else if(lineIndex == 1)
                 workedhrs = Int.Parse(line);
              lineIndex++;
          }
          rdr.Close();
      }
  }
Comment
Ali-hatem

People who like this

1 Show 5 · 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 MccoringPL · Sep 06, 2017 at 05:18 PM 0
Share

Ok, im checking it now, i will write instantly if that works and if not ;P

avatar image MccoringPL · Sep 06, 2017 at 05:37 PM 0
Share

Something goes wrong: http://screenshot.sh/odyItkRfrsdoS

http://screenshot.sh/n9GGKRJ0l8XXk

http://screenshot.sh/m9nZiUN3DbKae

avatar image Iarus · Sep 06, 2017 at 10:12 PM 0
Share

In your code, the lineIndex++ is only done if lineIndex == 1. So you probably parse the first two lines with the first "if" and write in the moneyo variable twice. Then you parse the third line, it's not a number and the code crashes. Put the lineIndex after the "else if" block and it should work as expected.

... I should have used curly braces with the if else if blocks to make it more clear.

avatar image MccoringPL · Sep 07, 2017 at 01:27 PM 0
Share

Yeah that works! Thanks!

avatar image MccoringPL · Sep 08, 2017 at 12:44 AM 0
Share

And i've got some stupid shit :/

Screens: http://screenshot.sh/m9nZiUN3DbKae http://screenshot.sh/n9GGKRJ0l8XXk http://screenshot.sh/odyItkRfrsdoS

I don't know what is not working :/

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

142 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity save system only works once. 0 Answers

How to make a save script in javascript. 1 Answer

How to add a new string in a list of strings AND serialize the list everytime a new string is added to the list? 1 Answer

how to save image in app IOS 0 Answers

Saving, Loading and Editing levels of 2D Android Platformer (Now in SQLite) 1 Answer


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