- Home /
PlayerPrefs not working on android
Hi, I am working on a game and I am able to make a high score system using PlayerPrefs. But when I build my project to my android device the high score is the same as on my pc and when I get a higher score, the high score text doesn't update. Note this only happens on my android device. Everything works well on pc. How would I fix this? Any help is appreciated.
using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;
using System;
using System.Diagnostics;
public class Score : MonoBehaviour
{
public TextMeshProUGUI scoreText;
public TextMeshProUGUI scoreAtEndText;
public TextMeshProUGUI highscoreText;
public TextMeshProUGUI newBestText;
public Player player;
public float score;
public bool scoreIncrease = true;
void Start()
{
highscoreText.enabled = false;
highscoreText.text = "BEST: " + PlayerPrefs.GetFloat("Highscore", 0).ToString("0");
PlayerPrefs.Save();
highscoreText.ForceMeshUpdate(true);
highscoreText.enabled = true;
}
void Update()
{
if (scoreIncrease) { score += Time.deltaTime * 60; }
else { score += 0; }
scoreText.text = score.ToString("0");
scoreAtEndText.text = "SCORE: " + score.ToString("0");
if (score > PlayerPrefs.GetFloat("Highscore", 0))
{
PlayerPrefs.SetFloat("Highscore", score);
PlayerPrefs.Save();
highscoreText.text = "BEST: " + score.ToString("0");
newBestText.gameObject.SetActive(true);
}
}
}
Answer by dewaeq · Sep 25, 2020 at 04:46 PM
I think you got this problem because when testing your code in the editor, you never reset all the PlayerPrefs keys. So because the values already exist in the editor, there isn't a problem but when you start fresh (on android) there is.
Answer by l3mon · Sep 25, 2020 at 05:33 PM
In your case, you don't need a parameter for the .ToString() function I believe.
should be sufficient. Also in the Update() function you don't necessarily need the default value, as you set it in the Start() function earlier.PlayerPrefs.GetFloat("Highscore", 0).ToString();
Your answer
Follow this Question
Related Questions
how to save a highscore 1 Answer
How to save data without using PlayerPrefs? 2 Answers
How to access additional SharedPreferences on Android 0 Answers
Player prefs easy highscore? 1 Answer
Write a new high score does not!!! 1 Answer