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 /
This question was closed Sep 23, 2013 at 12:17 PM by Ranger-Ori for the following reason:

The question is answered, right answer was accepted

avatar image
6
Question by Ranger-Ori · Sep 18, 2013 at 02:05 PM · androidfiletxt

Saving data in to files (Android)

Hey, I'm trying to save information on files using these methods, here's the code:

 using System.IO
 
 public class scoreFile
 {
     private int[] level;
     private bool[] completed;
     private float[] completionTime;
 
     private string[] scores;
 
     private string fileName;
 
 
     //This class stores information for each of the levels.
     public scoreFile(int num)
     {
         fileName = "scores.txt";
 
         try
         {
 
             if (!File.Exists(fileName))
             {
 
                 level = new int[num];
                 completionTime = new float[num];
                 completed = new bool[num];
                 scores = new string[num];
 
                 Debug.Log("Opened file!");
 
                 for (int i = 0; i < num; i++)
                 {
                     level[i] = i + 1;
                     completionTime[i] = 0;
                     completed[i] = false;
 
                     scores[i] = level[i] + " " + 0 + " " + completionTime[i];
                 }
 
                 Debug.Log("About to write into file!");
                 File.WriteAllLines(fileName, scores);
             }
 
             else
             {
                 Debug.Log("File is exist! Loading!");
                 loadFile();
             }
         }
 
         catch (System.Exception e)
         {
             Debug.Log(e);
         }
     }
 
     public bool isComplete(int i) { return completed[i]; }
     public int levels(int i) { return level[i - 1] + 1; }
     public float timeScore(int i) { return completionTime[i]; }
 
 
 
     public void loadFile()
     {
         Debug.Log("Reading");
         string[] levelsInfo = File.ReadAllLines(fileName);
 
         int num = levelsInfo.Length;
 
         level = new int[num];
         completionTime = new float[num];
         completed = new bool[num];
 
         for (int i = 0; i < num; i++)
         {
             //Debug.Log(levelsInfo[i]);
             string str = levelsInfo[i];
             level[i] = (int)int.Parse(str.Substring(0, 1));
             int f = int.Parse(str.Substring(2, 1));
             if (f == 0)
                 completed[i] = false;
             else
                 completed[i] = true;
             completionTime[i] = (float)float.Parse(str.Substring(4));
         }
     }
 }


It works great on Unity3D, but doesn't on my android mobile device (Nexus 4) is it because im using TXT extension? I don't understand whats the issue.

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 dudszim123 · Feb 17, 2018 at 02:15 PM 0
Share

where the files located in unity?

1 Reply

  • Sort: 
avatar image
14
Best Answer

Answer by Ranger-Ori · Sep 23, 2013 at 12:17 PM

Found the solution, should save the files in Application.persistentDataPath

which means the code should have looked like:

  private string filePath = Application.persistentDataPath + "/settings.txt";
Comment
Add comment · 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 isteffy · Jul 27, 2016 at 09:03 AM 0
Share

Also set the Write Access to External (SDCard) when you put the app on your phone. This option is found in Player Settings > Other Settings

avatar image enriqueflores · Aug 23, 2016 at 02:04 PM 2
Share

The line

 private string filePath = Application.persistentDataPath + "/settings.txt";

should replace this?

 File.WriteAllLines(fileName, scores);



avatar image JerryC92 enriqueflores · Jun 09, 2019 at 09:52 AM 0
Share

Did you figure out if your assumption was correct? The accepted answer was too vague.

avatar image enriqueflores JerryC92 · Jun 10, 2019 at 12:44 PM 0
Share

Yeah.. the right way to reference a path is with the line

private string filePath = Application.persistentDataPath + "/settings.txt";

Show more comments

Follow this Question

Answers Answers and Comments

20 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

Related Questions

file read/write in Android devices(phone/tablet...) 1 Answer

How to tell android to open a file 0 Answers

Android and XML (Reading/Wirting) 1 Answer

Dat File Location 1 Answer

Access file in Android with Streamreader 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