Search Unity

How can i add HighScore to my game?

Discussion in 'Scripting' started by JeeJDouwe, Sep 4, 2017.

  1. JeeJDouwe

    JeeJDouwe

    Joined:
    Sep 4, 2017
    Posts:
    2
    hello!

    i want to add a HighScore count to this script:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class Score : MonoBehaviour {
    5.  
    6.     public Transform player;
    7.     public Text scoreText;
    8.  
    9.     void Update () {
    10.  
    11.         scoreText.text = player.position.z.ToString("0");
    12.  
    13.     }
    14. }
    how can i do that?
     
  2. Sinci1

    Sinci1

    Joined:
    Feb 18, 2016
    Posts:
    53
    just a tip in the future, when making questions, make it a little detailed on what you want.
    But from what i gather, you want to have a number that shows the highest score,this is what i think you want :
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class Score : MonoBehaviour {
    5.  
    6.     public Transform player;
    7.     public Text scoreText;
    8.     public int HighestScore;  //This is the highest score
    9.     public int CurrentScore;
    10.  
    11.     void Update () {
    12.  
    13.         scoreText.text = player.position.z.ToString("0");//im assuming that Player.position.z is the score
    14.         ScoreCalculating ();
    15.     }
    16.     void ScoreCalculating(){
    17.         CurrentScore = player.position.z;// again im gonna assume that "player.position.z" is the current score;
    18.         if (CurrentScore > HighestScore){ //Basicly this says if the current score is higher than the old high score
    19.             HighestScore = CurrentScore;  // and then this updates that the highest score is now the current score
    20.         }
    21.     }
    22. //Also you can delete these messages, they are just there so you understand what is what
    23. }
    let me know if this works or not.
     
  3. JeeJDouwe

    JeeJDouwe

    Joined:
    Sep 4, 2017
    Posts:
    2
    as i copy this it gives this error:(20,40) CSO266: Cannot implicity convert 'float' to 'int'
     
  4. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    372
    Sounds like your player position is a float?
    Try replacing int with float here:

    public int HighestScore;
    public int CurrentScore;
     
  5. Sinci1

    Sinci1

    Joined:
    Feb 18, 2016
    Posts:
    53
    Oh oops my bad, do what BambooHutGames said :
    Code (CSharp):
    1.     using UnityEngine;
    2.     using UnityEngine.UI;
    3.    
    4.     public class Score : MonoBehaviour {
    5.    
    6.         public Transform player;
    7.         public Text scoreText;
    8.         public float HighestScore;  //i changed this
    9.         public float CurrentScore;  // and this from int to float, my mistake
    10.    
    11.         void Update () {
    12.    
    13.             scoreText.text = player.position.z.ToString("0");//im assuming that Player.position.z is the score
    14.             ScoreCalculating ();
    15.         }
    16.         void ScoreCalculating(){
    17.             CurrentScore = player.position.z;// again im gonna assume that "player.position.z" is the current score;
    18.             if (CurrentScore > HighestScore){ //Basicly this says if the current score is higher than the old high score
    19.                 HighestScore = CurrentScore;  // and then this updates that the highest score is now the current score
    20.             }
    21.         }
    22.     //Also you can delete these messages, they are just there so you understand what is what
    23.     }
    24.  
    again let me know if this work or not.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If you don't want your score to be a float, you can cast to int.
     
  7. sivillicasebastian

    sivillicasebastian

    Joined:
    Sep 17, 2018
    Posts:
    1
    Doesent work it wont load the script..
     
  8. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446