- Home /
i want to save high score but my code is not working???,i want to know where i have done mistake with this code . i want to save high score but i i restart the game this will reset??
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class score : MonoBehaviour {
 public Text scoretext;
 public int ballvalue;
 // Use this for initialization
 public int scorevalue;
 public gamecontroller GC;
 public Text highscore;
 void Start () {
     scorevalue = 0;
     highscore.text = PlayerPrefs.GetInt ("HIGH SCORE: \n",0).ToString();
     Updatescore ();
     updatehighscore ();
 }
 void OnTriggerEnter2D()
 {
     scorevalue += ballvalue;
     GC.timeleft = Time.deltaTime +20 ;
 
     Updatescore ();
     timer ();
 
 }
 void OnCollisionEnter2D(Collision2D collision){
 
     if (collision.gameObject.tag == "Bomb") {
     
         scorevalue -= ballvalue ;
     
         Updatescore ();
     }
 }
 
 // Update is called once per frame
 void Updatescore () {
 updatehighscore ();
     scoretext.text = "score:\n" + scorevalue;
 
     }
 void updatehighscore() {
 
     if (scorevalue > PlayerPrefs.GetInt ("HIGH score:\n", 0)) 
     {
         PlayerPrefs.SetInt ("HIGH SCORE:\n", scorevalue);
         highscore.text = scorevalue.ToString ();
     }
     }
 void timer()
 {
     GC.timertext.text = "Time Left:\n" + Mathf.RoundToInt (GC.timeleft);
 }
} ,
Answer by Happeloy · May 19, 2018 at 05:54 PM
You need to call
 PlayerPrefs.Save();
after your call PlayerPrefs.SetInt(); to actually save the score.
Edit: you also need to be consistent when getting/setting the values, it's case sensitive. You are using three different strings here, "HIGH SCORE: \n", "HIGH score:\n" and "HIGH SCORE:\n", they need to be identical to all point to the same thing. And you don't need to put the \n in there.
Your answer
 
 
             Follow this Question
Related Questions
Saving score and applying highscore (C#) 2 Answers
Recognising high score when updated with a graphic 0 Answers
Letter Ranking System, Checking Higher Rank 1 Answer
Saving Highscore 2 Answers
Loading a score on a scene 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                