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. Dismiss Notice

[SOLVED] PlayerPrefs only working in Editor, not Built

Discussion in 'Android' started by Leeee, Aug 10, 2014.

  1. Leeee

    Leeee

    Joined:
    Aug 10, 2014
    Posts:
    3
    hi

    Player Settings is on "Prefer External" and the App has Write Access to External.

    I know it is very unclean and at first I had a Script on its own to handle highscore attached to the TextMesh but i thought maybe break it down to the most simple procedure for Debugging.
    I started Unity (and JS) last Monday just for training a bit.


    Code (JavaScript):
    1. #pragma strict
    2. public static var hit=false;
    3. var PlayAgain : GameObject;
    4. var highscore_text_mesh : TextMesh;
    5. var highscore_text : String;
    6. var score :float;
    7.  
    8. function Start()
    9. {
    10.     highscore_text_mesh.text = "Highscore: "+PlayerPrefs.GetString("highscore");
    11.     hit=false;
    12. }
    13.  
    14. function OnCollisionEnter2D(coll: Collision2D)
    15. {
    16.     if (coll.gameObject.tag == "Enemy")
    17.     {
    18.         hit=true;
    19.         /*Destroy(gameObject);
    20.         Destroy(this);
    21.         Destroy(rigidbody2D);    */
    22.         highscore();
    23.  
    24.         Instantiate(PlayAgain, Vector2 (0, 0), Quaternion.identity);
    25.     }
    26. }
    27.  
    28. function OnGUI ()
    29. {
    30.  
    31.     if (hit==true)
    32.     {
    33.         if (GUI.Button (Rect (Screen.width/2-75,Screen.height/2-25,150,50), "Play Again"))
    34.         {
    35.             Zeit_vergangen.elapsedTime=0;
    36.             hit=false;
    37.             Application.LoadLevel (0);
    38.         }
    39.     }
    40. }
    41.  
    42. function highscore()
    43. {
    44.     highscore_text=PlayerPrefs.GetString("highscore");
    45.     score=Zeit_vergangen.elapsedTime;
    46.  
    47.     if ((PlayerPrefs.HasKey("highscore"))&& (parseFloat(highscore_text)<score))
    48.     {
    49.         PlayerPrefs.SetString("highscore",score.ToString());
    50.         highscore_text=score.ToString();
    51.     }
    52.  
    53.     highscore_text_mesh.text = "Highscore: "+ highscore_text;
    54.     PlayerPrefs.Save();
    55. }
    ps: how can i get the buttom to be same size on every device ? it looks great on my pc but much too small on my Tablet.
     
  2. Dzxyan

    Dzxyan

    Joined:
    Sep 23, 2013
    Posts:
    167
    actually what you problem you asking?
     
  3. Leeee

    Leeee

    Joined:
    Aug 10, 2014
    Posts:
    3


    It simply wont Get, Set the "Highscore" in PlayerPrefs on Android. Only in Editor
     
  4. Dzxyan

    Dzxyan

    Joined:
    Sep 23, 2013
    Posts:
    167
    you code write
    Code (CSharp):
    1. if ((PlayerPrefs.HasKey("highscore"))&& (parseFloat(highscore_text)<score))
    2.     {
    3.         PlayerPrefs.SetString("highscore",score.ToString());
    4.         highscore_text=score.ToString();
    5.     }
    if no have key, how it SetString for it?
     
  5. Disolution

    Disolution

    Joined:
    Feb 24, 2014
    Posts:
    71
    I'm kinda new as well in this area but why dont you try to make the app create aactual file within its app data path (been reading this aspect for android and found out Application.persistentDataPath would do the work) and each time u set or get a score u just do a StreamReader/StreamWriter function for a "scores.txt"?
     
  6. Leeee

    Leeee

    Joined:
    Aug 10, 2014
    Posts:
    3
    i hate myself right now.
    DZXYAN you are right.
    Changed to much at once so this slipped through
    Solved
     
  7. Dzxyan

    Dzxyan

    Joined:
    Sep 23, 2013
    Posts:
    167
    It's ok,
    that always happen for newbie