Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Android save / load file

Discussion in 'Android' started by Dic747oR, Jan 24, 2016.

  1. Dic747oR

    Dic747oR

    Joined:
    Nov 14, 2015
    Posts:
    6
    Hello, I just made application that saves score into a text file, it works on PC but not on android. I also tried to search whole memory of phone but file wasn't made. In script is the same path for checking the file if it exist (if it doesn't, then it creates one at the same path as the app is loading and saving (path to file is string variable). Can you help me? Do I need to add some rights to this app to open that or it's just not working on a phone? I'm using System.IO.File.blablabla for working with files
     
  2. leoncorrl

    leoncorrl

    Joined:
    Apr 27, 2014
    Posts:
    15
  3. hiimachicken

    hiimachicken

    Joined:
    Apr 25, 2014
    Posts:
    12
    You could use playerprefs for example for saying and loading score,
    its very simple when you get to understand it.

    Code (CSharp):
    1. //save score
    2. void SaveScore(){
    3. score +=1;
    4. PlayerPrefs.SetInt("Score"score);
    5.  
    6. }
    7. //load score
    8. void LoadScore(){
    9. int getScore = PlayerPrefs.GetInt("Score");
    10. Score = getScore;
    11. }
    12.  
    13.  

    of course you will need to implement this into your own code

    EDIT*

    To check if the file exists then you will do the following

    Code (CSharp):
    1. //load score
    2. void LoadScore(){
    3.  
    4. if(PlayerPrefs.HasKey("Score")){//the key has been found
    5. int getScore = PlayerPrefs.GetInt("Score");
    6. Score = getScore;
    7. }else{
    8. print("No Key Found");
    9. }
    10. }
     
    Last edited: Jan 25, 2016