- Home /
Saving score and applying highscore (C#)
Hi,
I have a score script which works great but the thing is i want to implement highscore, which means i have to save my score. The thing is i have looked online but there scripts are so different to mine. Do you think i can implement a highscore to my game so when the scene restarts the highscore stays.
CODE
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Player : MonoBehaviour
{
public Text countText;
private int count;
public Text scoreText;
private int scount;
public AudioClip coin;
public AudioClip enemy;
void Start ()
{
count = 0;
SetCountText ();
scount = 0;
SetScoreText ();
}
void update () {
}
void OnTriggerEnter2D(Collider2D col)
{
if(col.gameObject.tag == "Coin")
{
col.gameObject.SetActive (false);
AudioSource.PlayClipAtPoint(coin, transform.position);
count = count + 1;
SetCountText ();
scount = scount + 1;
SetScoreText ();
} else if (col.gameObject.tag == "Enemy")
{
AudioSource.PlayClipAtPoint(enemy, transform.position);
Destroy(gameObject);
scoreText.gameObject.SetActive (true);
}
}
void SetCountText ()
{
countText.text = " " + count.ToString ();
}
void SetScoreText ()
{
scoreText.text = " " + scount.ToString ();
}
}
Answer by jprocha101 · Jul 26, 2015 at 10:40 PM
You can use player prefs for that. In your Start method load the high score. In your Update method once the score is greater than the high score then save the new score as the high score.
Under the functions section there is an example of each method. Drilling down into each link, reading the description and exa$$anonymous$$ing the example is a great way to learn :)
Answer by Hexer · Jul 26, 2015 at 10:47 PM
Something like this?
int Highscore;
void Start(){
//Each time the scene starts it will get the Highscore that was stored in the playerPrefs.
Highscore = PlayerPrefs.GetInt("Highscore",0);
}
void Update(){
//if the variable Score from another script is lower or equal to Highscore,
//set the HighScore tobe equal to the Score.
//PlayerPref.SetInt
if(ScoreScript.Score > Highscore){
Highscore = ScoreScript.Score;
PlayerPref.SetInt("Highscore", Highscore);
}
}
If the highscore is higher or equal to the Score than the Highscore is equal to the score. It will then set the int in the PlayerPref. This would safe the highscore.
Here the Scorescript stands for the name of the script where you access Score. Score is a static public int in that Scorescript. (I didn't know how you call it in your script so I just called it Score to be direct)
Don't you have the IF condition reversed? You would want to set the high score if your ScoreScript.Score > Highscore
Well you can put it in a custom function of its own.
void Custom(){
if(ScoreScript.Score > Highscore){
Highscore = ScoreScript.Score;
PlayerPref.SetInt("Highscore", Highscore);
}
}
And call it when the game ends.
Custom();
EDIT : UHH$$anonymous$$ yes you are right (misread reversed for reserved..) . I changed it, my bad. (it's 01:25am in my country and Im still working on my game and answering questions xD, gotta nap soon I guess.
Your answer
Follow this Question
Related Questions
Creating score based on time with PlayerPrefs 1 Answer
whats wrong with my high score code? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Highscore not working 1 Answer