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

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

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

  1. phantom_x404

    phantom_x404

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

    Code (CSharp):
    1. using UnityEngine;
    2.     using UnityEngine.UI;
    3.     public class Score : MonoBehaviour
    4.     {
    5.         public Transform player;
    6.         public Text ScoreValue; //means, input is required, since a variable has been created
    7.         // Update is called once per frame
    8.         void Update()
    9.         {
    10.             ScoreValue.text = ((player.position.z-15)/10).ToString("0"); //ToString("0") is to remove all decimal points
    11.             PlayerPrefs.SetString("CurrentScore", ((player.position.z-15)/10).ToString("0"));
    12.         }
    13.     }
    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:

    Code (CSharp):
    1. using UnityEngine;
    2.     using UnityEngine.UI;
    3.     public class GameOverScore : MonoBehaviour
    4.     {
    5.         public Text OverScore;
    6.         void Update()
    7.         {
    8.      
    9.             OverScore.text = PlayerPrefs.GetFloat("CurrentScore").ToString();    
    10.         }
    11.     }
    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.
     
  2. taqiarzoo424

    taqiarzoo424

    Joined:
    Mar 8, 2020
    Posts:
    23
    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

    Code (CSharp):
    1. using UnityEngine;
    2.     using UnityEngine.UI;
    3.     public class Score : MonoBehaviour
    4.     {
    5.         public Transform player;
    6.         public Text ScoreValue; //means, input is required, since a variable has been created
    7.         // Update is called once per frame
    8.         void Update()
    9.         {
    10.             ScoreValue.text = ((player.position.z-15)/10).ToString("0"); //ToString("0") is to remove all decimal points
    11.             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.
    12.         }
    13.     }
    14. //PlayeCurrentScore class
    15. Using System.Collections;
    16. using UnityEngine;
    17.  
    18. public static class PlayeCurrentScore {
    19.         string CurrentScore=" ";
    20. }
    21.  
    22.  
    23. // your Game over script using UnityEngine;
    24.  using UnityEngine.UI;
    25. public class GameOverScore : MonoBehaviour
    26.    {
    27.        public Text OverScore;
    28.      void Update()
    29.         {
    30.      
    31.             OverScore.text =PlayeCurrentScore.CurrentScore;
    32.  
    33.       }
    34.   }
    35.  
    36.  
    37.  
    Hope this will solve your issue
     
  3. taqiarzoo424

    taqiarzoo424

    Joined:
    Mar 8, 2020
    Posts:
    23

    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

    Code (CSharp):
    1.  
    2. Before making a transition to game over scene update the static Class
    3.  
    4. Code (CSharp):
    5. using UnityEngine;
    6.     using UnityEngine.UI;
    7.     public class Score : MonoBehaviour
    8.     {
    9.         public Transform player;
    10.         public Text ScoreValue; //means, input is required, since a variable has been created
    11.         // Update is called once per frame
    12.         void Update()
    13.         {
    14.             ScoreValue.text = ((player.position.z-15)/10).ToString("0"); //ToString("0") is to remove all decimal points
    15.             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.
    16.         }
    17.     }
    18. //PlayeCurrentScore class
    19. Using System.Collections;
    20. using UnityEngine;
    21. public static class PlayeCurrentScore {
    22.         string CurrentScore=" ";
    23. }
    24. // your Game over script
    25.  
    26. using UnityEngine;
    27. using UnityEngine.UI;
    28. public class GameOverScore : MonoBehaviour
    29.    {
    30.        public Text OverScore;
    31.      void Update()
    32.         {
    33.    
    34.             OverScore.text =PlayeCurrentScore.CurrentScore;
    35.       }
    36.   }
    37.  
     
    phantom_x404 likes this.
  4. phantom_x404

    phantom_x404

    Joined:
    Apr 26, 2020
    Posts:
    26
    Thank You! I actually got it to work with PlayerPrefs, Thanks for your input :)