Search Unity

Help me finnish my game - Highscore script

Discussion in 'Scripting' started by Chos89, Aug 29, 2014.

  1. Chos89

    Chos89

    Joined:
    Jul 19, 2014
    Posts:
    6
    I'm doing an infinite runner type game for android, I got 3 scenes and I need a highscore system.
    I would figure it on my own but I just started with programming a few weaks ago and I need this game finnished soon.
    My first scene:
    start.PNG

    Here I need to call the saved highest score and displayed it here

    2nd (game) scene:

    game.PNG

    Here I need an updated score by deltaTime during the game until the player is destroyed/collided

    End scene:

    end.PNG

    Here I need to show the current score that the player achieved.

    And here is my script so far, I'm having problem setting it up and updating the text correctly:

    Code (CSharp):
    1. public class ScoreScript : MonoBehaviour {
    2.  
    3.     static float score = 0;
    4.     static float highscore = 0;
    5.  
    6.     void Start ()
    7.  
    8.     {
    9.         highscore = PlayerPrefs.GetFloat("highscore");
    10.  
    11.     }
    12.  
    13.  
    14.  
    15.     void Update ()
    16.     {
    17.         score += Time.deltaTime * 10;
    18.         if (score > highscore)
    19.             highscore = score;
    20.     }
    21.  
    22.     void OnDestroy()
    23.     {
    24.      
    25.         PlayerPrefs.SetFloat("score", Time.deltaTime * 10);
    26.      
    27.     }
    28. }
    29.  
    Thanks for your help and please bear in mind that I'm a huge noob and probably cant figure some "simple" things on my own.
     
  2. bbQsauce

    bbQsauce

    Joined:
    Jun 29, 2014
    Posts:
    53
    Your code
    Code (CSharp):
    1. public class ScoreScript : MonoBehaviour {
    2.     static float score = 0;                // there's no need of static variables
    3.     static float highscore = 0;    
    4.     void Start ()
    5.     {
    6.         highscore = PlayerPrefs.GetFloat("highscore");
    7.     }
    8.     void Update ()
    9.     {
    10.         score += Time.deltaTime * 10;
    11.         if (score > highscore)          // there's no need of checking this every frame
    12.             highscore = score;         // there's no need of  assigning a highscore every frame
    13.     }
    14.     void OnDestroy()
    15.     {
    16.  
    17.         PlayerPrefs.SetFloat("score", Time.deltaTime * 10); // you are setting the score as delta time ( time between frames) when this object is destroyed
    18.  
    19.     }
    20. }
    This should work.

    Code (CSharp):
    1. public class ScoreScript : MonoBehaviour {
    2.  
    3.     private float score = 0;          
    4.     private float highscore = 0;    
    5.  
    6.     void Start ()
    7.  
    8.     {
    9.  
    10.         highscore = PlayerPrefs.GetFloat("highscore");
    11.  
    12.     }
    13.  
    14.  
    15.  
    16.     void Update ()
    17.     {
    18.         score += Time.deltaTime * 10;
    19.  
    20.     }
    21.  
    22.     void OnDestroy()
    23.     {
    24.              if (score > highscore)    
    25.                {
    26.                        highscore = score;        
    27.                        PlayerPrefs.SetFloat("highscore", highscore);
    28.                  }
    29.  
    30.     }
    31. }
    Can i ask you what's the event ( trigger, collision, etc. ) that make you destroy the object?
     
  3. Chos89

    Chos89

    Joined:
    Jul 19, 2014
    Posts:
    6
    Hey, thanks, now I need a way to change the text to see if the score system is working, any idea?

    Haven't yet made the event, I was thinking of making a function OnCollider2D load.scene(3), thoughts?
    Do i need to unload the previous scene then?
     
  4. bbQsauce

    bbQsauce

    Joined:
    Jun 29, 2014
    Posts:
    53
    Hi, to check if the script is working just change the variables from private to public and see them changing in the inspector.

    To change a GUIText at runtime
    Code (CSharp):
    1.  
    2. public GUIText scoreText;
    3.  
    4. void Update()
    5. {
    6.    score=Time.deltaTime*10;
    7.    scoreText.text = ""+score;
    8. }
    9.  
    I'm not really into 2D so i can't really help you there.

    Anyways, if i can, i think you should concentrate more on unity and coding basics before getting yourself into a project. It's fine if you're doing this for practice :p
     
  5. Chos89

    Chos89

    Joined:
    Jul 19, 2014
    Posts:
    6
    This is for practice but I have a very strong reason why I need this finished soon, basically I need to have an app in my account.

    Anyway, the score system is now semi-working, few things need to be fixed:
    1. The time should be displayed as an integer, without decimal points
    2. The score is calculated/running at all scenes, not just the 2nd game scene, maybe I need to implement this somewhere:

    Time.timeSinceLevelLoad.ToString()

    3. Changing gui text real time I get this error: the variable scoreText hasnt been asigned.
    I guess I have to drag the text I want to change into this script but for some reason I cant
     
  6. bbQsauce

    bbQsauce

    Joined:
    Jun 29, 2014
    Posts:
    53
    Hi, to display only integers try to use ToString("0") instead, even if i'm not sure this will work
    Code (CSharp):
    1. public GUIText scoreText;
    2. void Update()
    3. {
    4.    score=Time.deltaTime*10;
    5.    scoreText.text=score.ToString("0");
    6. }
    The score is running in every scene where the script is attached to an object, so you should attach it only to your player in the playing scene. This is the full script

    Code (CSharp):
    1. public class ScoreScript : MonoBehaviour {
    2.     private float score = 0;        
    3.     private float highscore = 0;  
    4.     public GUIText=scoreText;
    5.     void Start ()
    6.     {
    7.         highscore = PlayerPrefs.GetFloat("highscore");
    8.     }
    9.     void Update ()
    10.     {
    11.         score += Time.deltaTime * 10;
    12.         scoreText.text=score.ToString("0");
    13.     }
    14.     void OnDestroy()
    15.     {
    16.              if (score > highscore)  
    17.                {
    18.                        highscore = score;      
    19.                        PlayerPrefs.SetFloat("highscore", highscore);
    20.                  }
    21.     }
    22. }
    As for the 3rd point, you must create a GUIText ( Game Object > Create other > GUIText ) , position it as you wish in the scene, and finally drag it from the hierarchy to the script in the inspector.
     
  7. Chos89

    Chos89

    Joined:
    Jul 19, 2014
    Posts:
    6
    Thanks bbQ for helping, you helped me a ton. The score system is finally working, I had to make some changes to the script because I'm using unity 4.6 with the new UI system.
    In the end I made 3 scripts, one for each scene and I'll post them here if anyone wants to use them.

    Note that you have to use using UnityEngine.UI;

    First script for the 1st scene that gets and displays the high score:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class HighscoreScript : MonoBehaviour {
    6.  
    7.            
    8.     public float highscore = 0;
    9.     public Text hscoreText;
    10.    
    11.    
    12.     void Start ()
    13.     {
    14.         highscore = PlayerPrefs.GetFloat("highscore");
    15.    
    16.         hscoreText.text=highscore.ToString("0");
    17.     }
    18. }
    19.  
    Second script that sets the score and the high score:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class ScoreScript : MonoBehaviour {
    6.     public float score = 0;      
    7.     public float highscore = 0;
    8.     public Text scoreText;
    9.  
    10.    
    11.     void Update ()
    12.     {
    13.         score += Time.deltaTime * 10;
    14.         scoreText.text=score.ToString("0");
    15.     }
    16.     void OnDestroy()
    17.     {
    18.         PlayerPrefs.SetFloat("score", score);
    19.  
    20.         if (score > highscore)
    21.         {
    22.             highscore = score;    
    23.             PlayerPrefs.SetFloat("highscore", highscore);
    24.         }
    25.     }
    26.  
    And the final script that shows the current score at the end:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class EndscoreScript : MonoBehaviour {
    6.     public float score = 0;      
    7.     public float highscore = 0;
    8.     public Text scoreText;
    9.    
    10.    
    11.     void Start ()
    12.     {
    13.         score = PlayerPrefs.GetFloat("score");
    14.        
    15.         scoreText.text=score.ToString("0");
    16.     }
    17.  
    18. }
     
  8. bbQsauce

    bbQsauce

    Joined:
    Jun 29, 2014
    Posts:
    53
    You're welcome!