Search Unity

Saving HighScore problem

Discussion in '2D' started by divyakarthik, May 29, 2017.

  1. divyakarthik

    divyakarthik

    Joined:
    May 29, 2017
    Posts:
    2
    Hello guys,
    i am new to unity .i am making a 2d topdown puzzle game . i am currrently having a little problem saving the high score.
    my game score system works like this... scorecount decreases when time(seconds) increases.the resultant decreased scorecount will be added to a base score which is set to 100. and then that score will be saved as high score if the score > highscore. in my case the highscore should be 0 at the beginning. the problem is that the highscore at the beginning is showing a random number of 3099 (i've tried resetting the playerprefs using Playerprefs.DeleteAll(); ). here is my scoremanager script.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class ScoreManager : MonoBehaviour {


    public Text scoreText;
    public Text BestScoreText;
    private float score;
    public float scoreCount;
    public float pointsPerSecond;





    // void start()
    // {
    //
    //
    // }

    void Update () {
    scoreCount += pointsPerSecond * Time.deltaTime;
    score = 100;
    }

    void FixedUpdate()
    {

    score = score + scoreCount;


    scoreText.text = "Your Score : " + Mathf.Round (score);

    BestScoreText.text = "Best Score : " + ((int)PlayerPrefs.GetFloat ("BestScore"));

    //PlayerPrefs.DeleteAll ();

    if (score > PlayerPrefs.GetFloat ("BestScore")) {
    PlayerPrefs.SetFloat ("BestScore", score);
    }

    }


    }
     
  2. divyakarthik

    divyakarthik

    Joined:
    May 29, 2017
    Posts:
    2
    and another thing. whenever i increase the scorecount . default best score increases. like this ... if i set it to 10 ,best score showing 249, 100 means 1699, 200 means 3099. so what mistake i have been doing? and how to rectify it ?.thank you in advance.
     
  3. StormHerald

    StormHerald

    Joined:
    May 29, 2017
    Posts:
    17
    Please check this forum post to show codes properly:
    https://forum.unity3d.com/threads/using-code-tags-properly.143875/


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ScoreManager : MonoBehaviour {
    7.  
    8.  
    9. public Text scoreText;
    10. public Text BestScoreText;
    11. private float score;
    12. public float scoreCount;
    13. public float pointsPerSecond;
    14.  
    15.  
    16.  
    17.  
    18.  
    19. // void start()
    20. // {
    21. //
    22. //
    23. // }
    24.  
    25. void Update () {
    26. scoreCount += pointsPerSecond * Time.deltaTime;
    27. score = 100;
    28. }
    29.  
    30. void FixedUpdate()
    31. {
    32.  
    33. score = score + scoreCount;
    34.  
    35.  
    36. scoreText.text = "Your Score : " + Mathf.Round (score);
    37.  
    38. BestScoreText.text = "Best Score : " + ((int)PlayerPrefs.GetFloat ("BestScore"));
    39.  
    40. //PlayerPrefs.DeleteAll ();
    41.  
    42. if (score > PlayerPrefs.GetFloat ("BestScore")) {
    43. PlayerPrefs.SetFloat ("BestScore", score);
    44. }
    45.  
    46. }
    47.  
    48.  
    49. }
    You had two variables to keep the score. you also set the score variable to 100 every update, so it will always stay like the "random number" with your code.
    Instead of that, try this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ScoreManager : MonoBehaviour {
    7.  
    8. //For the UI
    9. public Text scoreText;
    10. public Text BestScoreText;
    11.  
    12. //The actual score
    13. public float scoreCount;
    14.  
    15. //Score to add every second
    16. public float pointsPerSecond;
    17.  
    18. //You can also do this in Update()
    19. void FixedUpdate()
    20. {
    21.  
    22. //Instead of having score and scoreCount, use only
    23. //scoreCount to make this calculation
    24. scoreCount += pointsPerSecond * Time.deltaTime;
    25.  
    26. //ScoreCount is used to show the current score
    27. scoreText.text = "Your Score : " + Mathf.Round (scoreCount);
    28.  
    29. //do this first to update the high score
    30. if (score > PlayerPrefs.GetFloat ("BestScore"))
    31. {
    32. PlayerPrefs.SetFloat ("BestScore", scoreCount);
    33. }
    34.  
    35. //show the high score
    36. BestScoreText.text = "Best Score : " + ((int)PlayerPrefs.GetFloat ("BestScore"));
    37.  
    38. }
    39.  
    40.  
    41. }