Score value in GameOver screen is 0 instead of final value from the Game Level scene

I am trying to make an infinite runner, in it there are two scenes, one is game scene and the other is GameOver scene. In the game scene the score is counted live as the player moves along the z axis using the following code:

using UnityEngine;
    using UnityEngine.UI;
    public class Score : MonoBehaviour
    {
        public Transform player;
        public Text ScoreValue; //means, input is required, since a variable has been created
        // Update is called once per frame
        void Update()
        {
            ScoreValue.text = ((player.position.z-15)/10).ToString("0"); //ToString("0") is to remove all decimal points
            PlayerPrefs.SetString("CurrentScore", ((player.position.z-15)/10).ToString("0"));
        }
    }

Since this is on the Game Level scene, the score is calculated according to player movement by dragging the player object into the script.
When game ends, the scene transiitions to the GameOver screen using SceneManagement.
Since I want to display the score from Game Level in the Game over scene, I use a prefab to store the score.
Now in the Gameover scene I have a text object which I wanted to be updated with the final value of score form the Game Level, so i made a script for it which is as follows:

using UnityEngine;
    using UnityEngine.UI;
    public class GameOverScore : MonoBehaviour
    {
        public Text OverScore;
        void Update()
        {
      
            OverScore.text = PlayerPrefs.GetFloat("CurrentScore").ToString();     
        }
    }

Here, you have to drag the text(score value) which you want to keep updating whenever the level ends.
The default score value in game over scene is set to: -
Using this, the score text in Game Over scene should get updated to the final score of the game scene.
But, whenever the game ends and there is a transition to the Game Over scene, the score in Game Over scene changes from: ‘-’, to: 0.
But when the level ended, the score was not 0, can someone tell me what to do or what code to use so that the value in Game Over scene is updated to final value of Game Level scene? I would really appreciate if you could help me out.

I don’t know how to do it with prefab
But I can tell you the alternate solutions
Which is create a static Class to store the score
Before making a transition to game over scene update the static Class

using UnityEngine;
    using UnityEngine.UI;
    public class Score : MonoBehaviour
    {
        public Transform player;
        public Text ScoreValue; //means, input is required, since a variable has been created
        // Update is called once per frame
        void Update()
        {
            ScoreValue.text = ((player.position.z-15)/10).ToString("0"); //ToString("0") is to remove all decimal points
            PlayeCurrentScore.CurrentScore= ((player.position.z-15)/10).ToString("0"));// instead of doing this per Frame you can do this once when to make transaction to game over scene. 
        }
    }
//PlayeCurrentScore class
Using System.Collections;
using UnityEngine;

public static class PlayeCurrentScore {
        string CurrentScore=" ";
} 


// your Game over script using UnityEngine;
 using UnityEngine.UI;
public class GameOverScore : MonoBehaviour 
   { 
       public Text OverScore;
     void Update()
        {
     
            OverScore.text =PlayeCurrentScore.CurrentScore;

      } 
  }

Hope this will solve your issue

I don’t know how to do it with prefab
But I can tell you the alternate solutions
Which is create a static Class to store the score
Before making a transition to game over scene update the static Class

Before making a transition to game over scene update the static Class

Code (CSharp):
using UnityEngine;
    using UnityEngine.UI;
    public class Score : MonoBehaviour
    {
        public Transform player;
        public Text ScoreValue; //means, input is required, since a variable has been created
        // Update is called once per frame
        void Update()
        {
            ScoreValue.text = ((player.position.z-15)/10).ToString("0"); //ToString("0") is to remove all decimal points
            PlayeCurrentScore.CurrentScore= ((player.position.z-15)/10).ToString("0"));// instead of doing this per Frame you can do this once when to make transaction to game over scene.
        }
    }
//PlayeCurrentScore class
Using System.Collections;
using UnityEngine;
public static class PlayeCurrentScore {
        string CurrentScore=" ";
}
// your Game over script

using UnityEngine;
using UnityEngine.UI;
public class GameOverScore : MonoBehaviour
   {
       public Text OverScore;
     void Update()
        {
    
            OverScore.text =PlayeCurrentScore.CurrentScore;
      }
  }
1 Like

Thank You! I actually got it to work with PlayerPrefs, Thanks for your input :slight_smile: