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 c0dysmyth · Jul 18, 2019 at 03:03 PM · scorescore systemhighscore

Adding High Score to Game menu from existing code?

Hey i have read through a bunch of similar questions asked but I am having trouble applying it to my existing code. I would like my high score to be displayed on the game menu but I don't know how to extract the highest score from the score code i am using now. I know i have to set player prefs and compare high score to regular score but i am having trouble wrapping my head around it. My score code is as follows:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Score : MonoBehaviour
 {
     public static int scoreValue = 0;
     Text score;
     // Start is called before the first frame update
     void Start()
     {
         score = GetComponent<Text>();
         scoreValue = 0;
     }
 
     // Update is called once per frame
     void Update()
     {
         score.text = "Score: " + scoreValue;
     }
 
 
  
 }

Then I have that linked to when a game object (platform) gets destroyed i add 10 points. not sure if thats important but that code is as follows:

 sing System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Eraser1 : MonoBehaviour
 {
     // Start is called before the first frame update
 
     void OnTriggerEnter2D(Collider2D o)
     {
 
         if (o.tag == "turf1")
         {
             Score.scoreValue += 10;
             Destroy(o.gameObject);
         }
     }
 
     // Update is called once per frame
     void Update()
     {
       
     }
 }

Any help is greatly appreciated.

Comment
Add comment · Show 1
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 NilaTeju · Jul 18, 2019 at 04:17 PM 0
Share

You just have to set the highscore after the game is over. Get the current highscore using something like PlayerPrefs.TryGetValue("Highscore",0); If your Score.scoreValue is higher than that, set the new score using something like PlayerPrefs.SetInt("Highscore", Score.scoreValue);

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Vega4Life · Jul 18, 2019 at 04:15 PM

I would make your Score class into a singleton, then let your Eraser (or whatever is giving points) call into it. The singleton will deal with the score, highscore, saving, etc. Here is an example:


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 
 /// <summary>
 /// Just place this in your scene somewhere and let objects send it points via UpdateScore method
 /// </summary>
 public sealed class ScoreManager : MonoBehaviour
 {
     // Simple singleton pattern
     static ScoreManager instance;
     public static ScoreManager Instance
     {
         get
         {
             if (instance == null)
             {
                 instance = FindObjectOfType<ScoreManager>();
             }
 
             return instance;
         }
     }
 
     [SerializeField] Text highScoreText;
     [SerializeField] Text currentScoreText;
 
     int currentScore;
     public int CurrentScore
     {
         get { return currentScore; }
         private set
         {
             currentScore = value;
             UpdateScoreText();
         }
     }
 
     // When the game launches, set the high score
     private void Awake()
     {
         SetHighScoreText();
     }
 
     // Just for testing - should delete the entire method
     // Uncomment to use this as a test
     //private void Update()
     //{
     //    if (Input.GetKeyDown(KeyCode.Space))
     //    {
     //        UpdateScore(10);
     //    }
     //    else if (Input.GetKeyDown(KeyCode.Return))
     //    {
     //        CheckForNewHighScore();
     //    }
     //}
 
 
     // Call this method to add more points (which will auto update the text for you)
     // Called like this ScoreManager.Instance.UpdateScore(10); from wherever points are given
     public void UpdateScore(int delta)
     {
         CurrentScore += delta;
     }
 
 
     private void UpdateScoreText()
     {
         currentScoreText.text = CurrentScore.ToString();
     }
 
 
     private void SetHighScoreText()
     {
         // We try to find a highscore, if there isn't one, we default to 0
         int highScore = PlayerPrefs.GetInt("HighScore", 0);
 
         highScoreText.text = highScore.ToString();
     }
 
 
     // Game ended - call this to set new highscore if there is one (and save it)
     public void CheckForNewHighScore()
     {
         int highScore = PlayerPrefs.GetInt("HighScore", 0);
         if (currentScore > highScore)
         {
             PlayerPrefs.SetInt("HighScore", currentScore);
             PlayerPrefs.Save();
 
             // Update high score text
             SetHighScoreText();
         }
     }
 }
 

No need to use an update method for scores. Just let the property deal with it when its updated. This makes sure that whenever a score gets updated, so does the text.

Hope this helps a little.

Comment
Add comment · Show 2 · 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 c0dysmyth · Jul 18, 2019 at 05:44 PM 1
Share

this is genius! thank you. I implemented this script swapped a few things out and now my high score saves to my game menu! Thank you so much.

avatar image Vega4Life c0dysmyth · Jul 18, 2019 at 05:49 PM 0
Share

No problem.

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

112 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 avatar image avatar image avatar image avatar image

Related Questions

Score and Highscore - Highscore not saving, loading, or displaying. 0 Answers

Storing high score 2 Answers

Score and HighScore saving script [please help] 2 Answers

How to save a high score 2 Answers

How to increase score by one per second when you are holding an object 2 Answers


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