Question by
Omega2077 · Nov 05, 2015 at 06:48 AM ·
playerprefsscriptingbasicshighscorespersistentdatapath
How do I incorporate the PlayerPrefs.Save so that it writes all the modified data to disk. Note I am very new to C# and thank you all in advance
using UnityEngine; using System.Collections;
public class Score : MonoBehaviour {
static int score = 0;
static int highScore = 0;
static public void AddPoint() {
score++;
if(score > highScore) {
highScore = score;
}
}
void Start() {
highScore = PlayerPrefs.GetInt("highScore", 0);
}
void OnDestroy() {
PlayerPrefs.SetInt ("highScore", highScore);
}
void ExitGame() {
highScore = PlayerPrefs.Save (PlayerPrefs.SetInt ("highscore"));
}
void Update () {
GetComponent<GUIText> ().text = "Score: " + score + "\nHigh Score: " + highScore;
}
}
Comment
Answer by Levithan6785 · Nov 05, 2015 at 06:47 PM
This is very simple to do actually.
When loading the scores:
void Awake()
{
highScore = PlayerPrefs.GetInt("name"); //Grabs an integer in playerPrefs by the name of "name"
}
So set an integer to be saved
PlayerPrefs.SetInt("name", score); //Sets an integer to the playerPref named "name". Then score is the value you are saving
Wiki
Answer by Omega2077 · Nov 06, 2015 at 04:07 AM
Thank you
Hi again I did try that code but it did not work tried a few other things but still no success, I could really use more ideas on how to do this.
What I need to know is how to SetInt and GetInt through persistence data, does anyone know how?
Your answer
Follow this Question
Related Questions
how do i store highscore locally C# (SIMPLE) 3 Answers
Highscore not working 0 Answers
Help making a high score with player prefs/displaying it 1 Answer