Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How to display final game over score in Another scene

Discussion in 'Scripting' started by phantom_x404, Apr 26, 2020.

  1. phantom_x404

    phantom_x404

    Joined:
    Apr 26, 2020
    Posts:
    26
    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:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class Score : MonoBehaviour
    5. {
    6.  
    7.     public Transform player;
    8.     public Text ScoreValue;
    9.  
    10.     void Update()
    11.     {
    12.         ScoreValue.text = ((player.position.z-15)/10).ToString("0");
    13.     }
    14. }
    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:
    Code (CSharp):
    1.  
    2. PlayerPrefs.SetInt("CurrentScore", player.position.y);
    3.  
    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.
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    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.
     
    phantom_x404 and Kurt-Dekker like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    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...

    2. 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.
     
    phantom_x404 likes this.
  4. phantom_x404

    phantom_x404

    Joined:
    Apr 26, 2020
    Posts:
    26
    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:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. public class Score : MonoBehaviour
    5. {
    6.     public Transform player;
    7.     public Text ScoreValue; //means, input is required, since a vriable has been created
    8.     // Update is called once per frame
    9.     void Update()
    10.     {
    11.         ScoreValue.text = ((player.position.z-15)/10).ToString("0");
    12.         PlayerPrefs.SetFloat("CurrentScore", ((player.position.z-15)/10)));
    13.     }
    14. }
    15.  
    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.
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. public class GameOverScore : MonoBehaviour
    5. {
    6.     public Text OverScore;
    7.     void Update()
    8.     {      
    9.         PlayerPrefs.GetFloat("CurrentScore", 0);    
    10.     }
    11. }
    12.  
    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?
     
  5. phantom_x404

    phantom_x404

    Joined:
    Apr 26, 2020
    Posts:
    26
    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)
     
  6. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    https://docs.unity3d.com/ScriptReference/PlayerPrefs.GetFloat.html

    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)
     
    phantom_x404 likes this.
  7. phantom_x404

    phantom_x404

    Joined:
    Apr 26, 2020
    Posts:
    26
    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.
     
  8. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    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.
     
    phantom_x404 likes this.
  9. phantom_x404

    phantom_x404

    Joined:
    Apr 26, 2020
    Posts:
    26
    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:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class Score : MonoBehaviour
    5. {
    6.  
    7.     public Transform player;
    8.     public Text ScoreValue; //means, input is required(the text that needs to be constantly updated i.e. score), since a variable has been created
    9.  
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.         int scoreValInt = Mathf.RoundToInt((player.position.z - 18) / 20);
    14.         //Debug.Log(scoreValInt);
    15.  
    16.         ScoreValue.text = ((player.position.z-18)/20).ToString("0"); //to string 0 to remove all decimal points
    17.         PlayerPrefs.SetString("CurrentScore", (scoreValInt.ToString("0"))); //earlier it was .z-15/10
    18.  
    19.         PlayerPrefs.SetInt("IntHighScore", scoreValInt);
    20.         Debug.Log(PlayerPrefs.GetInt("IntHighScore"));
    21.  
    22.         if (scoreValInt > PlayerPrefs.GetInt("IntHighScore"))
    23.         {
    24.             PlayerPrefs.SetString("HighScore", (scoreValInt.ToString("0")));
    25.         }
    26.        
    27.     }
    28. }
    For Displaying High Score on Game Over scene:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class HighScore : MonoBehaviour
    5. {
    6.  
    7.     public Text ValueToUpdate;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         ValueToUpdate.text = PlayerPrefs.GetString("HighScore", "0").ToString();
    13.     }
    14.  
    15. }
    16.  
    But for some reason the score in High Score is stuck at 21, any solution for this? :-/
     
    Last edited: Apr 28, 2020
    medianuk and bhatiahitika4 like this.
  10. bhatiahitika4

    bhatiahitika4

    Joined:
    Mar 20, 2022
    Posts:
    1
    hey thanks!! this is exactly what I was looking for