- Home /
How to save data without using PlayerPrefs?
Hello everybody, I read all questions, answers and guides about playerprefs that I could find, nothing helped me, so Im asking if there is another easy way to save data. So you knew what Im trying to do here is my code:
var controlTexture : Texture2D;
var controlTexture1 : Texture2D;
var Score :int;
var Enemy : Transform;
var dist:int =1;
var lost = false;
var customSkin :GUISkin;
var highscore:int;
function start(){
highscore = PlayerPrefs.GetInt("HighscoreSTR");
}
function OnGUI () {
GUI.skin = customSkin;
GUI.Box(new Rect(10,10,150,45), Score.ToString());
GUI.Box(new Rect(10,60,150,45), highscore.ToString());
if (lost) {
GUI.Box(new Rect(Screen.width/2 -207 ,Screen.height/2 -95 ,420,200),"Well Played!");
if (GUI.Button (Rect (Screen.width/2 -200,Screen.height/2 + 50,150,50), controlTexture)){
Application.LoadLevel(1);
}
if (GUI.Button (Rect (Screen.width/2 +50,Screen.height/2 + 50,150,50), controlTexture1)){
Application.LoadLevel(0);
}
GUI.Label (Rect (Screen.width/2 -20 ,Screen.height/2 -50,500,500), Score.ToString());
}
}
function Update () {
if (Score >= highscore){
highscore = Score;
PlayerPrefs.SetInt("HighscoreSTR", highscore);
PlayerPrefs.Save();
}
dist = Vector3.Distance(Enemy.position, transform.position);
if (dist <=2){
lost = true;
}
if (lost){
Time.timeScale = 0;
}
else{
Time.timeScale = 1;
}
for (var touch : Touch in Input.touches) {
if (touch.phase == TouchPhase.Began) {
if (dist ==3){
Score = Score + 5 ;
}
if (dist ==4){
Score = Score + 3 ;
}
if (dist ==5){
Score = Score + 1 ;
}
}
}
}
Answer by prankard · Feb 26, 2015 at 03:25 PM
Hi Milerael.
Your PlayerPrefs code is actually working the way it should expect.
However, as your 'start' function is lowercase. It isn't running and thus not loading the highscore.
Change
function start(){
into
function Start(){
Also, please consider changing:
if (Score >= highscore){
into
if (Score > highscore){
To prevent your score being set to the same number on Update.
O$$anonymous$$G! Thanks so much, I can't believe I didn't notice that!
It usually the simplest errors that break everything spectacularly
Answer by cariaga · Feb 26, 2015 at 03:11 PM
here is a unity xml example
http://wiki.unity3d.com/index.php?title=Saving_and_Loading_Data:_XmlSerializer
or you can do an sqlite approach http://wiki.unity3d.com/index.php/SQLite
Your answer
Follow this Question
Related Questions
how to save a highscore 1 Answer
PlayerPrefs not working on android 2 Answers
Player prefs easy highscore? 1 Answer
How to access additional SharedPreferences on Android 0 Answers
Playerprefs don't reset after reinstalling the build on my phone. 1 Answer