- Home /
My HighScore Stuck on a ceratin value and wont update.(in game HighScore works, only problem when game is restarted the value of highscore is fixed)
My highscore after game loads is stuck on 100. while it should have updated with latest value; I have made 2 set of codes one for score and other for high score so i am retriving the value from score into highscore.I am new to unity and I am sorry if i voilated any rules. Thanks Again. my score code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameScore : MonoBehaviour {
public GameObject Highscorer;
Text scoreTextUI;
int score;
public int Score{
get{
return this.score;
}
set
{
this.score = value;
if (Highscorer.GetComponent<highscore> ().HScore < this.score) {
Highscorer.GetComponent<highscore> ().HScore =this.score;// getting the highscore value from score
}
UpdateScoreTextUI ();
}
}
// Use this for initialization
void Start () {
scoreTextUI = GetComponent<Text> ();
}
// Update is called once per frame
void UpdateScoreTextUI () {
string scorestr = string.Format ("{0:0000000}", score);
scoreTextUI.text = scorestr;
}
}
High score code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class highscore : MonoBehaviour {
Text highscorer;
static int hscore;
public int HScore{
get{
return hscore;
}
set
{
hscore = value;
UpdateScoreTextUI ();
}
}
// Use this for initialization
void Start () {
highscorer = GetComponent<Text> ();
highscorer.text = PlayerPrefs.GetInt ("highscore").ToString ();
}
// Update is called once per frame
void UpdateScoreTextUI () {
string hscorestr = string.Format ("{0:0000000}", hscore);
highscorer.text = hscorestr;
PlayerPrefs.SetInt (hscorestr, hscore);
}
Comment