- Home /
How to set high score and have it displayed
I have a score in my game that increments at certain points! this is working fine but i want a saved high score and unsure how to implement playerprefs as i am still getting to grips with unity and c#! so far i have placed a ui text element in my canvas and this is called High Score: 0 i would like the 0 to hold and save the high score! the working score that increments at the moment is in the update method. i just really dont know how to add the player prefs and add it to the text element on the game screen! using unity 5.3.5
public class Player : MonoBehaviour {
public string currentColor;
public float jumpForce = 10f;
public Rigidbody2D circle;
public SpriteRenderer sr;
public Color blue;
public Color yellow;
public Color pink;
public Color purple;
public GameObject obsticle;
public GameObject colorChanger;
public static int score = 0;
public Text scoreText;
public static int highScore;
public Text highScoreText;
void Start () {
setRandomColor ();
circle.isKinematic = true;
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Jump") || Input.GetMouseButtonDown (0))
{
circle.isKinematic = false;
circle.velocity = Vector2.up * jumpForce;
}
//scoreText.text = score.ToString ();
scoreManager();
hiScoreManager ();
}
public void scoreManager(){
scoreText.text = score.ToString ();
}
public void SaveHighScore(){
highScore = PlayerPrefs.GetInt ("HighScore");
if (highScore < score) {
highScore = score;
PlayerPrefs.SetInt ("HighScore", highScore);
}
}
public void hiScoreManager(){
highScoreText.text = highScore.ToString ();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Scored")
{
score++;
Destroy (collision.gameObject);
Instantiate (obsticle, new Vector2(transform.position.x,transform.position.y + 7f), transform.rotation);
return;
}
if (collision.tag == "ColorChanger")
{
setRandomColor ();
Destroy (collision.gameObject);
Instantiate(colorChanger, new Vector2(transform.position.x,transform.position.y + 7f), transform.rotation);
return;
}
if (collision.tag != currentColor) {
Debug.Log ("You Died");
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
score = 0;
}
if (collision.tag == "Floor")
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
void setRandomColor()
{
int rand = Random.Range (0, 4);
switch (rand)
{
case 0:
currentColor = "Blue";
sr.color = blue;
break;
case 1:
currentColor = "Yellow";
sr.color = yellow;
break;
case 2:
currentColor = "Pink";
sr.color = pink;
break;
case 3:
currentColor = "Purple";
sr.color = purple;
break;
}
}
}
Answer by PersianKiller · Sep 05, 2017 at 04:20 PM
you should not check the value of score in your update :)
you just need a function to set the value of your score. every time that the player gets score this function should be called.
something like this
public void scoreManager(){
scoreText.text=score;
}
every time you get score call this function .dude I'm not good at English but i suppose you need a highScore function.
public void SaveHighScore(){
highScore=PlayerPrefs.GetInt ("HighScore");
if(highScore<Score){
highScore=Score;
PlayerPrefs.SetInt ("HighScore",highScore);
}
then create another function to put the value of highScore in some text.hope it helps .because it helped me :)
Hi and thank you for your reply! i will try that! how would i add this to the text ui element that is in the canvas! at the moment it stays at High Score: 0 very greatful for your help
hi dude . you already did it ,I wonder why you asked this :D .
scoreText.text = score.ToString ();
I think you just need a function to do it,you're checking score at update function.
you can create another text ui ,then put your highScore in that.
public Text t1;
void Start(){
t1 = GetComponent<Text> ();
int HighScore=PlayerPrefs.GetInt("HighScore"); }
public void ShowHighScore(){
t1.text = ""+HighScore;
}
put it in the update function :) GOODLUC$$anonymous$$
thank you very much you have gave me something to go on! i appreciate it :-)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity 5 2D C# interactive grass 2 Answers
How to make the tower shoot at the one who is closer to the finish line? 0 Answers