How to display final game over score in Another scene

Hello,
I am trying to make a game in unity where the score is counted based off of the distance traveled by the player along the Y axis.
I have two scenes in the game, one is the Game Level scene, and the other scene is the Game Over scene.
On the Main scene, the score is counted and continuously updated as the player moves along the Y axis, and when the player hits an obstacle, the game ends and it transitions to the game over scene(using: SceneManagement).
Now I want to display the final score that the player has achieved in the Game Level scene, on the Game Over scene.
My code for displaying the live score in Game Level scene is given below:

using UnityEngine;
using UnityEngine.UI;

public class Score : MonoBehaviour
{

    public Transform player;
    public Text ScoreValue;

    void Update()
    {
        ScoreValue.text = ((player.position.z-15)/10).ToString("0");
    }
}

In the above code, we need to specify the player and he text that needs to be updated, but this cannot be done so in the Game Over scene since here there is no player, there only exists a restart button, and other options(Quit, Main Menu).
So I tried to reference this variable in the script which I used to display the score in Game Over scene using:

PlayerPrefs.SetInt("CurrentScore", player.position.y);

What I am trying to do here is use a UI text element and using the script give it the updated score value.

But this gives me another error saying: float cannot be converted to int

Keeping in mind that I am a beginner and fairly new to C#(though I have done Python programming)what would be the best solution to this? It’d be Amazing if I could receive a solution/reply at the earliest since I have taken this on as a project for myself during Quarantine.

player.position.y returns a float number, not an integer. You can see that by hovering over the ‘y’ letter with your mouse inside of your editor. To fix this, simply use PlayerPrefs.SetFloat and PlayerPrefs.GetFloat instead of SetInt and GetInt.

2 Likes

The .y component comes out of player.position as a float.

You are storing it using a method called SetInt().

You can either:

  1. start using the .SetFloat() method (if you want that)

OR…

  1. convert player.position.y to an integer before sending it to .SetInt()

You can convert to an integer in a number of ways, depending on how you want to handle rounding. This can be called ‘casting’ or ‘converting’ or ‘flooring.’

Try googling up ‘C# cast float to int’ and you’ll see some possibilities for #2 above.

1 Like

Hello, thank you for taking the time to help me out, could you be kind enough to explain how I would get this value to update in the Gameover scene?

The updated code for main scene is:

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

Now in the game over scene I have added a script to the Text which is called game over score, now I want the text to be updated every time the game ends, but in my case it remains static.

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

The game over score text is set to: -
I want it to update when the game ends, but the value for score text in the Game Over scene does not update,what should I do for this?

Thank You! It worked, but now the value does not update in the game over screen though there are no errors in the code(not that the compiler or unity shows any)

GetFloat() returns a float value when you call it. You are not assigning it to anything in your Update(). Try:
OverScore.text = PlayerPrefs.GetFloat("CurrentScore", 0).ToString();

Also. Don’t use PlayerPrefs inside of Update. They might impact your performance. Instead, call them when you need to save the score (like when you are about to go into the next scene) and when you want to load the score (like inside of Awake() or Start() in your new scene)

1 Like

I did that and t worked(sorry forgot to edit the comment lol), now the value of - changes when the Game Level ends and Game Over scene is displayed, but it always changes to 0 and not the final value from Game Scene? The updated value is not displayed, it just displays 0 as score.

It’s a small issue that’s up to you to resolve. It’s a bug, just use breakpoints to see what’s going wrong.

1 Like

Forgive me for my ignorance but I have NO IDEA what break points are(maybe because I am a beginner and do not have much knowledge about it, or I just need to do some more research), so could you tell me how to use breakpoints ? I used debug.log to check the score in both scemes, in Game Level scene it shows the score properly, but in the Game Over scene… :-/, I’d appreciate if you’d help me out.

EDIT: Thank You, I figured it out, the error was with the PlayerPref part, I had set it as String but asked to get it as float.
But now I am trying to set up a high score system, but the text always remains stuck at 21, any Idea why?

The code is given below:

For setting HIGH SCORE and normal SCORE:

using UnityEngine;
using UnityEngine.UI;

public class Score : MonoBehaviour
{

    public Transform player;
    public Text ScoreValue; //means, input is required(the text that needs to be constantly updated i.e. score), since a variable has been created

    // Update is called once per frame
    void Update()
    {
        int scoreValInt = Mathf.RoundToInt((player.position.z - 18) / 20);
        //Debug.Log(scoreValInt);

        ScoreValue.text = ((player.position.z-18)/20).ToString("0"); //to string 0 to remove all decimal points
        PlayerPrefs.SetString("CurrentScore", (scoreValInt.ToString("0"))); //earlier it was .z-15/10

        PlayerPrefs.SetInt("IntHighScore", scoreValInt);
        Debug.Log(PlayerPrefs.GetInt("IntHighScore"));

        if (scoreValInt > PlayerPrefs.GetInt("IntHighScore"))
        {
            PlayerPrefs.SetString("HighScore", (scoreValInt.ToString("0")));
        }
       
    }
}

For Displaying High Score on Game Over scene:

using UnityEngine;
using UnityEngine.UI;

public class HighScore : MonoBehaviour
{

    public Text ValueToUpdate;

    // Start is called before the first frame update
    void Start()
    {
        ValueToUpdate.text = PlayerPrefs.GetString("HighScore", "0").ToString();
    }

}

But for some reason the score in High Score is stuck at 21, any solution for this? :-/

2 Likes

hey thanks!! this is exactly what I was looking for