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
2
Question by Lootheo · Apr 07, 2014 at 01:18 AM · androidfilestream

Why file creation on android is not working?

So I have a code that reads a high score and also saves it, it works fine while testing the game on the pc but the moment I send it to android it stops working, everything else is fine.

 public void Save(int a)
     {
         score1 = a;
         //Debug.Log (score1);
         BinaryFormatter bf = new BinaryFormatter ();
         FileStream file = File.Create(Application.persistentDataPath + "/playerScore.dat");
 
         PlayerData puntaje = new PlayerData ();
         puntaje.score = score1;
         bf.Serialize (file, puntaje);
         file.Close();
     }
     public int Load()
     {
         if (File.Exists (Application.persistentDataPath + "/playerScore.dat")) {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath+ "/playerScore.dat", FileMode.Open);
             PlayerData puntaje = (PlayerData)bf.Deserialize(file);
             file.Close();
             score1 = puntaje.score;
             }
         return score1;
     }
Comment
Add comment · Show 7
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 Jun Hin · Apr 07, 2014 at 01:10 PM 0
Share

A much simpler way of saving a high score is by using PlayerPrefs, which will work on all platforms.

avatar image Lootheo · Apr 08, 2014 at 03:19 AM 0
Share

I tried that method too

avatar image Jun Hin · Apr 08, 2014 at 04:37 AM 0
Share

How did you go about coding it?

In the script that is counting the score, also have a high score variable. Then in your Update or Save function, you would have something like:

 if(score > highScore)
 {
   highScore = score
   PlayerPrefs.SetInt("High Score", highScore);
 }

Then wherever you want to display or load the high score just use PlayerPrefs.GetInt("High Score")

avatar image supernat · Apr 08, 2014 at 04:50 AM 0
Share

According to the Unity reference, the files will be stored "in a public area" on the filesystem. This usually means SD card. Do you have the WRITE_EXTERNAL_STORAGE permission in your manifest?

avatar image Lootheo · Apr 08, 2014 at 05:15 AM 0
Share

compilign to try Jun Hin method, according to me I have the permission, in player settings-> other settings-> configuration I have the write access as External(sd card) unless there is something else im missing, which will make me facepalm until the end of time

Edit: didnt work either, still works perfectly on unity editor but not on android

Edit2: when i try to install the apk it tells me Allow this application to: Storage-$$anonymous$$odify/delet sd card contents, I'm really giving up

Show more comments

1 Reply

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

Answer by Tomer-Barkan · Apr 08, 2014 at 06:56 AM

Application.persistentDataPath in android refers to the public (external) storage, which is the SD card. For this to work, you must specify that the application requires SD card write access. You can do this in the build menu, select android, click player settings, and in other settings there is a parameter called "Write Access", make sure it is set to "External".

Your other option would be to use Application.dataPath instead, and it will write it in the internal memory belonging to the application.

Comment
Add comment · Show 3 · 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 Lootheo · Apr 09, 2014 at 12:59 AM 0
Share

I have done around 10 different methods, persistentDataPath, player prefs, data path, write acces internal and external , text file ins$$anonymous$$d of binary, and combinations of all of the above, ALL of them works on my pc but not on the android phone ($$anonymous$$e or any1 else) .

avatar image Tomer-Barkan · Apr 09, 2014 at 07:25 AM 0
Share

Ok, In that case I'd start investigating other parts of my code. Add a debug log, check if the save/load methods are even called on the android device.

avatar image huyakinemota · Dec 19, 2017 at 11:58 AM 1
Share

switching to External helped me. thanks

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

26 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

Related Questions

Reading from .CSV works fine on windows editor, and no data on device (Android) 2 Answers

Can't read lines from file by using StreamReader on Android platform 2 Answers

How to convert WWW type to FileStream? 1 Answer

Android Persistent Data loading fails 1 Answer

File paths on Android 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