Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Aug 13, 2017 at 01:15 PM by Oskiek for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Oskiek · May 12, 2016 at 07:58 PM · c#androidscripting problem

Code not working as it should

Hello. I made a code that should make an text file and store language name in it. And it works, it makes a file, and writes language in, but when i want to load this file and read language in another scene, it doesnt work. Here is a piece of script and how i use it:

 public void Start ()
 
  {
      StreamReader r = File.OpenText(Application.persistentDataPath + "//" + "jezyk.txt");
      string jez = r.ReadToEnd ();
      r.Close();
      jezyk = jez;
      if (jezyk.Equals ("polish")) {
          guzik1.GetComponentInChildren<Text> ().text = "Szybka gra";
      } else if (jezyk.Equals ("english")) {
          guzik1.GetComponentInChildren<Text> ().text = "Quick game";
      } else if (jezyk.Equals ("russian")) {
          guzik1.GetComponentInChildren<Text> ().text = "Быстрая игра";
      }
Comment
Add comment · Show 1
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 Bunny83 · May 12, 2016 at 08:27 PM 0
Share

It would help to know how you actually write the text to the file.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · May 13, 2016 at 12:40 AM

Well, as i said guessed in my comment you're using WriteLine. WriteLine does write an additional carriage return + line feed at the end of the text. So the file doesn't only contain the text you've written into but also the new line characters. When you do ReadToEnd() you actually read in the whole file which includes the new line characters. That's why i suggested to print the string length as well. The string length should tell you if the character count is right.

Furthermore you actually overwrite your text file before you read it because you have those lines in front of the reading code:

 StreamWriter w;
 w = f.CreateText();    
 w.Close ();

Those lines will create a new text file that will overwrite the existing file, so you end up with an empty file.

To avoid all those issues you could simply use File.WriteAllText and File.ReadAllText. Those two methods will write (and overwrite if necessary) the whole file and read the whole file.

 // write
 File.WriteAllText(Application.persistentDataPath + "//" + "jezyk.txt", "polish");
 
 
 // read
 string jez = File.ReadAllText(Application.persistentDataPath + "//" + "jezyk.txt");

There's no need for deleting the file if you just want to overwrite it.

Instead of writing a file with a single word in it you could simply use PlayerPrefs.

 // write
 PlayerPrefs.SetString("language", "polish")

 // read
 string jez = PlayerPrefs.GetString("language", "english");

GetString takes a "key" and a default value in case "language" doesn't exist yet.

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

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with inventory asset's script and its working 0 Answers

How to use the GPGS ISavedGameMetadata? 1 Answer

How to replace space keyboard with a simple tap on Android? 0 Answers

Help! Values from previous Serialized List<> shows up again after a while using Coroutine 0 Answers

Not able to retrieve information from script in unity. 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