PlayerScore prefab in unity gives value in gameover screen as 0, how do I show the final value in GameOver 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.
Your answer
Follow this Question
Related Questions
I need help with my script please 1 Answer
need help with code please 1 Answer
Score counter not working properly. 1 Answer
Highscore and PlayerPrefs unity C# 1 Answer