Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by andrewlewis379 · Aug 01, 2019 at 07:08 PM · highscores

How to add highscore to this script?

Hi there, i am trying for hours now but i can't find any help on how to add a highscore system to this specific concept. With this script my score goes up every second, and when the gameover scene is triggered, i basically want to take the achieved points and update my highscore text with it. Can anyone somehow walk me through? Thanks a lot.

 public class ScorePerSec : MonoBehaviour
 {
 
 public Text scoreText;
 public float score;
 public float pointIPS;
 
     void Start()
     {
         score = 0f;
         pointIPS = 3;
     }
 
     void Update()
     {
         scoreText.text = (int)score + " Points";
         score += pointIPS * Time.deltaTime;
     }
 
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by I_Am_Err00r · Aug 01, 2019 at 07:18 PM

  public class ScorePerSec : MonoBehaviour
  {
  
  public Text scoreText;
  public float score;
  public float pointIPS;
  public float highScore;
  
      void Start()
      {
        if(PlayerPrefs.GetInt("HighScore") != null)
        {
         highScore = PlayerPrefs.GetFloat("HighScore");
         }
          score = 0f;
          pointIPS = 3;
      }
  
      void Update()
      {
          scoreText.text = (int)score + " Points";
          score += pointIPS * Time.deltaTime;
          if(PlayerPrefs.GetFloat("HighScore") == null || score > highScore)
          {
              score = highScore;
              PlayerPrefs.SetFloat("HighScore", highScore);
          }
      }
  
  }

That isn't the most performant way to do this, but I don't know where your gameover screen chunk of code is, or if it's even part of this code, but I would move this block from Update to that section:

           if(PlayerPrefs.GetFloat("HighScore") == null || score > highScore)
              {
                  score = highScore;
                  PlayerPrefs.SetFloat("HighScore", highScore);
              }

Do that so it isn't constantly updating the highscore value when it doesn't need to and wasting resources.


FYI PlayerPrefs is a quick way to save data in Unity, that should work, don't have a compiler to test or double check for spelling errors.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrewlewis379 · Aug 01, 2019 at 07:38 PM 0
Share

Thank you, will look into it asap :)

avatar image I_Am_Err00r andrewlewis379 · Aug 01, 2019 at 07:45 PM 0
Share
 public class ScorePerSec : $$anonymous$$onoBehaviour
   {
   
   public Text scoreText;
   public float score;
   public float pointIPS;
   public float highScore;
   
       void Start()
       {
         if(PlayerPrefs.GetFloat("HighScore") != null)
         {
          highScore = PlayerPrefs.GetFloat("HighScore");
          }
           score = 0f;
           pointIPS = 3;
       }
   
       void Update()
       {
           scoreText.text = (int)score + " Points";
           score += pointIPS * Time.deltaTime;
           if(PlayerPrefs.GetFloat("HighScore") == null || score > highScore)
           {
               highScore = score;
               PlayerPrefs.SetFloat("HighScore", highScore);
           }
       }
   
   }
 

I told you I don't have a compiler! I saw a couple issues, this should be right now

avatar image andrewlewis379 · Aug 01, 2019 at 08:02 PM 0
Share

Thanks again, also had to replace the "null" with 0 because of float can't be converted to null or something error.

How can i now hook up my highscore text to this, so i can display the highscore as well?

Thanks for helping me out

avatar image I_Am_Err00r andrewlewis379 · Aug 01, 2019 at 08:09 PM 0
Share

Add a Text variable highScoreText, then at Start maybe do this:

 void Start()
        {
          if(PlayerPrefs.GetFloat("HighScore") != 0)
          {
           highScore = PlayerPrefs.GetFloat("HighScore");
           highScoreText.text = highScore;
           }
            score = 0f;
            pointIPS = 3;
        }

And then do something like this here:

 if(PlayerPrefs.GetFloat("HighScore") == null || score > highScore)
            {
                highScore = score;
                PlayerPrefs.SetFloat("HighScore", highScore);
                highScoreText.text = highScore;
            }

If you want it to constantly update, just add it Udate like you do with score, with a check that if the score exceeds the previous, then update the highScore.

avatar image andrewlewis379 · Aug 01, 2019 at 08:10 PM 0
Share

Ah i figured it out already, thanks a lot, you helped me a great deal. :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

108 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Wiki Highscores Problem 1 Answer

Displaying Highscores Problem 0 Answers

Score not resetting 3 Answers

Saving score and applying highscore (C#) 2 Answers

I need some help with my high score.,I need help with my highsore. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges