Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
1
Question by Omega2077 · Jul 08, 2015 at 04:13 PM · playerprefsscriptingbasicsscorehighscorescoin

How do I save highscore (Very Simple)

I have the scoring working just fine but when the game restarts it doesn't save the highscore and just resets everything back to 0. I am very new to coding in C# and would very much appreciate the help with getting this code correct so that it saves my highscore. The UI I am using is a text UI not a GUI. Thank you in advanced.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class ScoreManager : MonoBehaviour {
 
     public static int score;
 
     public static int highscore;
 
     Text text;
 
     void Start()
     {
         text = GetComponent<Text> ();
 
         score = 0;
 
         highscore = PlayerPrefs.GetInt ("highscore", highscore);
     }
 
     void Update()
     {
         if (score > highscore);
         highscore = score;
         text.text = "" + score;
 
         PlayerPrefs.SetInt ("highscore", highscore);
     }
 
     public static void AddPoints (int pointsToAdd)
     {
         score += pointsToAdd;
     }
 
     public static void Reset()
     {
         score = 0;
     }
 }
 
Comment
Add comment · Show 2
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 Hellium · Jul 08, 2015 at 03:24 PM 1
Share

You can't declare functions inside other functions in C#

(Start and OnDestroy inside Update)

avatar image Omega2077 · Jul 09, 2015 at 06:51 AM 0
Share

the score is displayed during game up the top left hand corner

4 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by maccabbe · Jul 09, 2015 at 04:31 PM

The code

 void Update(){
     if (score > highscore);
     highscore = score;
     text.text = "" + score;
  
     PlayerPrefs.SetInt ("highscore", highscore);
 }

is the same as

 void Update(){
     if (score > highscore){
     }
     highscore = score;
     text.text = "" + score;
  
     PlayerPrefs.SetInt ("highscore", highscore);
 }

that is, you're always setting highscore to the current score. Instead you probably want to use brackets as follows

 void Update(){
     if (score > highscore){
         highscore = score;
         text.text = "" + score;
  
         PlayerPrefs.SetInt ("highscore", highscore);
     }
 }

https://unity3d.com/learn/tutorials/modules/beginner/scripting/if-statements

You also probably want to set the text.text to highscore in start

 void Start()
 {
     text = GetComponent<Text> (); 
 
     score = 0;
  
     highscore = PlayerPrefs.GetInt ("highscore", highscore);
     text.text = highscore.ToString();
 }

Comment
Add comment · Show 1 · 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 abdo400 · Jul 23, 2016 at 04:45 PM 0
Share

Thank you so much. I have been struggling with this code for too long.

avatar image
1

Answer by Ryuzax · May 26, 2016 at 07:57 PM

try using:

 void OnDestroy()
 {
     PlayerPrefs.SetInt ("highscore", highscore);
     PlayerPrefs.Save();
 }

to actually save the PlayerPref. (hope it helps anyone with similar issues)

Comment
Add comment · Show 1 · 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 dofuk06 · Feb 18, 2018 at 09:00 PM 0
Share

$$anonymous$$an you saved my life :D thank you <3

avatar image
0

Answer by Dave-Carlile · Jul 08, 2015 at 04:34 PM

Where are you displaying the high score?

This line:

 text.text = "" + score; 


Is only ever displaying the score, and when you reload the game that's going to be 0 at the beginning. If you want it to display the high score then use that variable instead.

Also, to avoid confusion, you should get into the habit of indenting things properly. Instead of:

  void Update()
  {
      if (score > highscore)
           highscore = score;
      text.text = "" + score; }

Line up the braces...

 void Update()
 {
   if (score > highscore)
   {
     highscore = score;
   }
   text.text = "" + score;
 }

And your Awake function is indented too far. That combined with the brace at the end of the line in Update makes it look like you're declaring Awake inside of Update. Just a friendly tip on style that will make looking at your code much easier.

Comment
Add comment · Show 3 · 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 Omega2077 · Jul 09, 2015 at 06:50 AM 0
Share

See because my game is on a timer that goes for 4 $$anonymous$$utes once the timer hits 0 it auto resets the game and I want to keep my highscore, and that is the only issue I am having everything else works exactly like I intended it to. Just need to keep my highscore. The problem is this part of the code.

void Start() { highscore = PlayerPrefs.GetInt("highscore", 0); }

      void OnDestroy() {
          PlayerPrefs.SetInt("highscore", highscore);


avatar image Omega2077 · Jul 09, 2015 at 06:50 AM 0
Share

okay that makes sense but, just to ask quickly how should my code look because I want my highscore to still be there when the player play's the game again?

note: The code works perfect as it is, when you collect the coins it add points works like a charm just need the highscore to be saved so that you don't loose your highscore.

avatar image Dave-Carlile · Jul 09, 2015 at 04:07 PM 0
Share

@Omega2077 I've converted your answers to comments - discussion here is done through the comments and Answers are reserved for answers to questions.

avatar image
0

Answer by Pavlos_Mavris · May 17, 2020 at 03:47 PM

What if the user delete the game and then download it after 1 month will still be saved the high score?

Comment
Add comment · 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

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

9 People are following this question.

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

Related Questions

PlayerPrefs not saving my variable value for HighScore System 3 Answers

PlayerPref set int and get int 1 Answer

Is it necessary to create an Array() to show high scores in PlayerPref? 1 Answer

How many different scripts do I need to write to create a highscore system? 1 Answer

Recognising high score when updated with a graphic 0 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