• Unity
  • Services
  • Made with Unity
  • Learn
  • 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
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

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
4
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
11
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 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 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 1
Share

The line

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

should replace this?

 File.WriteAllLines(fileName, scores);



Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

19 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

Related Questions

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

Save/Copy a .txt file a runtime 1 Answer

How can i save objects to a file 1 Answer

Writing to a File Without Deleting What's Already There 2 Answers

Search for files on android 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges