- Home /
Question by
cankirici34 · Feb 09, 2021 at 08:31 AM ·
androidplayerprefsdatabasedatadata storage
Why PlayerPrefs not working although I use "HasKey" and Save() on android?
I know others have written this problem before but even if I use haskey or save my android device does not save data. its work fine on pc btw.
public class PlayerData : MonoBehaviour
{
public float HighScore { get; set; }
}
other script
public class PlayerPersistence : MonoBehaviour
{
public static void SavePlayerHighScore(float highScore)
{
if (PlayerPrefs.HasKey("HighScore"))
{
Debug.Log("has key");
}
else
{
PlayerPrefs.SetFloat("HighScore", highScore);
PlayerPrefs.Save();
}
}
public static PlayerData LoadData()
{
float highScore = PlayerPrefs.GetFloat("HighScore", 0);
PlayerPrefs.Save();
PlayerData playerData = new PlayerData()
{
HighScore = highScore
};
return playerData;
}
}
and calling like this
public PlayerData playerData { get; private set; }
private void Awake()
{
playerData = PlayerPersistence.LoadData();
HighScore();
t.text = playerData.HighScore.ToString();
}
Comment
In your SavePlayerHighScore function PlayerPrefs.HasKey("HighScore") is always true and code will never get to else which means you will never save score.
omg you are right thats worked thank you so much!
Your answer
Follow this Question
Related Questions
Storing Constant data in a mobile game 0 Answers
How to upgrade from free to paid version and maintain established player prefrence data? 0 Answers
is it safe to save info in PlayerPrefs? 2 Answers
Storing data on a server to be used for player currency in a multiplayer game 1 Answer
How do I go about using a server to store and retrieve data? 0 Answers