- Home /
How to use PlayerPrefs
Hey guys, i started to work on a simple game yesterday, and it's almost done, but i wanted to implement a Shop for the Player to buy things. I already made a system to store how many points the Player has, but i don't know how to save a float after the game is closed, and then restore it once it opens. I've been reading a little, and i discovered that there is one way, and you gotta use PlayerPrefs, but i never used it, and i didn't get what am i supposed to do and how. Could someone explain to me how would i use PlayerPrefs to save a variable once the game is closed, and then restore it once it is opened again?
Thank you kindly =]
Do something like..
float playerPoints = 100;
void SavePoints()
{
PlayerPrefs.SetFloat( "PlayerPoints", playerPoints );
}
void LoadPoints()
{
PlayerPrefs.GetFloat("PlayerPoints");
}
And access these methods through GUI buttons that save/load...But keep in $$anonymous$$d..Player prefs is for saving data such as Settings and player name and etc...The player can easily "hack" the player prefs file and change any value they want. I would reccommend saving and loading all data with X$$anonymous$$L Serialization.
Answer by Utopians · Apr 28, 2020 at 12:47 PM
Writing:
PlayerPrefs.SetInt("score",5);
PlayerPrefs.SetFloat("volume",0.6f);
PlayerPrefs.SetString("username","John Doe");
PlayerPrefs.Save();
// If you need boolean value:
bool val = true;
PlayerPrefs.SetInt("PropName", val ? 1 : 0);
PlayerPrefs.Save();
Reading:
int score = PlayerPrefs.GetInt("score");
float volume = PlayerPrefs.GetFloat("volume");
string player = PlayerPrefs.GetString("username");
bool val = PlayerPrefs.GetInt("PropName") == 1 ? true : false;
Answer by Commoble · Mar 13, 2017 at 01:25 AM
Playerprefs is meant for saving player preferences; you wouldn't want to use it to save high scores and progress data, because it's not difficult for players to edit the playerprefs data.
There's a couple of ways to save data safely, and this official unity tutorial video goes over one of them, using C#'s BinaryFormatter.
Answer by Alexei_UA · Mar 13, 2017 at 06:57 AM
At start of your game you use PlayerPrefs.GetInt("Player level"); to restore your player level (or something else).
Than you play your game and....
On disable() you use PlayerPrefs.SetInt("Player level", currentLevel); to write you current value (player level etc.).
It is an example of using PlayerPrefs with simple player level up system:
public class ExperienceLevelSystem : MonoBehaviour {
public static int currentXP;
private void Start()
{
if (PlayerPrefs.HasKey("currentXP"))
{
currentXP = PlayerPrefs.GetInt("currentXP");
}
Updated();
}
private void Update()
{
if (currentXP >= maxXP)
{
LevelUpSystem();
}
Updated();
}
public void LevelUpSystem()
{
//currentXP = currentXP - maxXP;
maxXP = 200 + 200 * (currentLevel * currentLevel);
currentLevel++;
levelUpMessage.SetActive(true);
Time.timeScale = 0;
public static void Add(int pointsToAdd)
{
currentXP += pointsToAdd;
}
void OnDisable()
{
PlayerPrefs.SetInt("currentLevel", currentLevel);
}
$$anonymous$$eep in $$anonymous$$d however, in this example..A player can easily find the file where the player prefs were saved and modify it easily to make themselves max level with no effort..Its standard practice to use player prefs only for player preferences such as Game Settings, $$anonymous$$ouse Bindings...ETC..Things a player would already have access to modify.
Answer by SuryaPrakashModi · Aug 17, 2020 at 02:06 PM
https://www.youtube.com/watch?v=GcnERRQ6K8Q
i think this video will be helpfull