Search Unity

Score to count from 0 to Total score at the end of game.

Discussion in 'Scripting' started by joshgozner, Mar 27, 2014.

  1. joshgozner

    joshgozner

    Joined:
    Mar 7, 2014
    Posts:
    7
    Hi I need help with the score for my game, I can get it to display the current score during playing the game but at the end
    of the game I would like to be able to have a way for it to recount the score from 0 to what you got.

    my current score script
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Score : MonoBehaviour {
    5.    
    6.  
    7.     public float highScore = 0;
    8.  
    9.  
    10.     void Start(){
    11.         highScore = PlayerPrefs.GetFloat ("highScore", 0);
    12.         }
    13.  
    14.     void OnDestroy(){
    15.         PlayerPrefs.SetFloat ("highScore", highScore);
    16.     }
    17.     void FixedUpdate(){
    18.         if (HeliMovement.cumulatedDistance > highScore) {
    19.             highScore = HeliMovement.cumulatedDistance;
    20.         }
    21.         }
    22.  
    23.     // Update is called once per frame
    24.     void Update () {
    25.  
    26.                 if (HeliMovement.gameStart == true) {
    27.  
    28.                         guiText.text = HeliMovement.cumulatedDistance.ToString ("f0") + "m";
    29.        
    30.                 }
    31.         }
    32. }
    33.  
    34.  
     
  2. joshgozner

    joshgozner

    Joined:
    Mar 7, 2014
    Posts:
    7
    Anyone?
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    maybe a little co-routine... starts at 0, then adds a small amount waits a fraction of a second in a while loop until the displayed scored is the total score.

    If you want it to tick up based on what actually happened (i.e. picked up 10 pts, then 50 pts, then 10 pts then ...) you'll need to store a list of score events and loop through that list.
     
  4. Bivrost

    Bivrost

    Joined:
    Mar 26, 2014
    Posts:
    80
    You could interpolate between 0 and the player's total score, e.g. using Mathf.Lerp or even Mathf.SmoothDamp. This way you can easily control the time/speed to accumulate the score. Do not wait between frames while accumulating the score, because in case the player reached a very high score, he will wait forever gazing at the screen, skeletonizing slowly...
     
  5. joshgozner

    joshgozner

    Joined:
    Mar 7, 2014
    Posts:
    7
    Thanks for your help :)
     
  6. joshgozner

    joshgozner

    Joined:
    Mar 7, 2014
    Posts:
    7
    i gave both a try but i got errors could you give me an example in c# please
     
  7. joshgozner

    joshgozner

    Joined:
    Mar 7, 2014
    Posts:
    7
    Somebody?
     
  8. jonSG

    jonSG

    Joined:
    Mar 26, 2014
    Posts:
    18
    This might get you going again:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class scoreBehavior : MonoBehaviour {
    6.     public bool finalizeScore = false;
    7.  
    8.     private int highScore = 0;
    9.     private int currentScore = 0;
    10.     private int displayScore = 0;
    11.  
    12.     private float replaySpeed = 0.1f;
    13.     private float replayPosition = 0.0f;
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.         finalizeScore = false;
    18.  
    19.         highScore = PlayerPrefs.GetInt ("highScore", 0);
    20.         currentScore = 0;
    21.         displayScore = 0;
    22.  
    23.         setScore (1000);
    24.     }
    25.    
    26.     // Update is called once per frame
    27.     void Update () {
    28.    
    29.         if (finalizeScore) {
    30.             if (replayPosition <= 1.0f) {
    31.                 displayScore = (int)Mathf.Lerp (0, currentScore, replayPosition);
    32.                 replayPosition += replaySpeed * Time.deltaTime;
    33.             } else {
    34.                 displayScore = currentScore;
    35.             }
    36.             guiText.text = string.Format ("final score: {0}\n high score {1}", displayScore, highScore);
    37.         }
    38.  
    39.     }
    40.  
    41.     public void setScore(int points){
    42.         currentScore = points;
    43.         if ( highScore < currentScore ){ highScore = currentScore; }
    44.         guiText.text = string.Format ("current score: {0}\n high score {1}", currentScore, highScore);
    45.     }
    46.  
    47. }
    48.