Home > Enterprise >  How do I save the score every time the scene resets and make it my new highscore when the score is h
How do I save the score every time the scene resets and make it my new highscore when the score is h

Time:12-10

I'm currently recreating Jetpack Joyride and I'm having trouble with adding a highscore. I currently track my score on where my player is on the Y position and placing the score on a canvas. So I was wondering how do I save the score everytime the score is higher that the highscore and when the scene resets.

This is what I currently use to track Y position of my player

public class Score : MonoBehaviour
{

    public Transform player;

    public Text scoreText;


    // Update is called once per frame
    void Update()
    {

        scoreText.text = player.position.x.ToString("0"   "M");

    }
}

CodePudding user response:

There is multiple ways to implement high score.

  1. You can use PlayerPrefs to store data in each scene and then load the data from storage and save again if the latest score is higher than the previous one.

  2. You can create a global object which is not destroyed when new scenes load. In that object, you can attach a high score script that will keep track of the score.

Example Script for the 2nd Option

using UnityEngine;
using System.Collections;


    public class MyCustomScript : MonoBehaviour {
    
        public int score = 0;

        void Awake()
        {
            GameObject[] objs = GameObject.FindGameObjectsWithTag("global");

            if (objs.Length > 1)
            {
                Destroy(this.gameObject);
            }

            DontDestroyOnLoad(this.gameObject);
        }
        
        // Update is called once per frame
        void Update () {
            if( score < **getScore()** ){
                score = getScore();
            }
        }

CodePudding user response:

You should be saving that value when your player lose the game look at the comments I added to understand.

public class Score : MonoBehaviour
    {
    
        public Transform player;
    
        public Text scoreText;
        public Text highScoreText;
    
        public float score;
        bool lost;
    
        private void Start()
        {
            HighScoreCheck();
        }
    
        void Update()
        {
            score = player.position.x; 
            //this is storing the score
            
            scoreText.text = score.ToString("0"   "M");
            
            
            highScoreText.text = PlayerPrefs.GetFloat("HighScore").ToString(); 
            //this is showing the highest score recorded
    
            LoseCheck();
            
    
        }
    
        private void HighScoreCheck()
        {
            if (!PlayerPrefs.HasKey("HighScore"))
                //checking if this key has any value saved to it
            {
               Debug.Log("No High Score recorded Yet");
            }
            else
            {
                Debug.Log("HighScore is : "   PlayerPrefs.GetFloat("HighScore"));
            }
        }
    
        private void LoseCheck()
        {
            if (lost)
            {
                if (score> PlayerPrefs.GetFloat("HighScore"))
                {
                    PlayerPrefs.SetFloat("HighScore", score);
                    //this is how you save a float/int into a key that is stored in the device
                }
                else
                {
                   Debug.Log("No new high score");
                }
    
            }
        }
    }
  • Related