Hi guys, working on a class project. Its a 2D shooter top down scroller. Right now I got the game working with some scaling problems but working as intended. Now I aim to store highscores.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreScript : MonoBehaviour {
public Text scoreText;
public int score = 0;
public void addScore(int amount) {
score += amount;
scoreText.text = "Puntuacion:
Well you’re already using PlayerPrefs do you have a reason not to use that to store it? You can store it with a simple Save method call after the set if the idea is local storage of the highscore.
You don’t have to call Save() every time you make a change, and the documentation states you should avoid calling it during gameplay. Save is called automatically by Unity, but you may want to periodically autosave in case the UnityPlayer/game/computer/device crashes unexpectedly, or when transitioning between scenes etc.
PlayerPrefs is conceptually meant to store Preferences (Hence “PlayerPrefs”). It’s not that robust to handle large amounts of information that typically goes with a save game but is fine for your use case.
When I say it’s not that robust, I can give you a concrete example. I was trying to be clever and making local savegames on a webplayer build using playerprefs. I had a chunk of binary data which I base64 encoded into a string. Then I could save the entire game like PlayerPrefs.SetString("Savegame", base64SaveGame);. This worked fine for a while, until the size of the save game reached about 250 kB. After that it stopped saving the game.
Note that anyone can manually edit the highscore “preference” on the system. Like on windows, it’ll be stored as plain text in the Registry. Though if you are just toying around and don’t really need any more complicated system for keeping track of your game state, then PlayerPrefs can be used this way.