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 /
  • Help Room /
avatar image
0
Question by El-Deiablo · May 14, 2016 at 08:38 PM · loadingsavingscene-switchinghighscores

Loading and Saving High Score

I have been working on this for hours, watched numerous tutorials, and searched unity answers, but cannot figure out what I'm doing wrong. I want to save my highscore using player prefs, but when I refresh the game the high score is back to 0. I am using two scripts:

Code#1:

 using UnityEngine;
 using System.Collections;
 using UnityEngine .UI;
 using UnityEngine .SceneManagement ;
 
 public class GAMEOVERManager : MonoBehaviour {
 
     public Text highScoreText;
     public Text scoreText;
     public Text goldTeethText;
 
     // Use this for initialization
     void Start () {
 
         highScoreText.text = PlayerPrefs.GetInt ("High Score").ToString ();
         scoreText.text =PlayerPrefs.GetInt ("Score").ToString() ;
         goldTeethText.text = PlayerPrefs.GetFloat ("Tooth Score").ToString ();
         PlayerPrefs.Save ();
 
     }
     
     // Update is called once per frame
     void Update () {
     
 
     }
 
     public void RestartGame(){
         SceneManager.LoadScene ("Gameplay");
     }
 
 
 }

Code#2:

 using UnityEngine;
 using System.Collections;
 using UnityEngine .UI;
 using UnityEngine .SceneManagement ;
 
 public class ScoreManager : MonoBehaviour {
 
     public static int highScoreCount;
 
     public static int scoreCount;
 
     public static int TKOCount;
 
     public static float toothCount;
 
     public Text TKOText;
 
     public Text scoreText;
 
 
     // Use this for initialization
     void Start () {
 
         PlayerPrefs.GetInt ("High Score");
 
         IntializeVariables ();
 
         TKOCount = 0;
         scoreCount = 0;
 
     }
     
     // Update is called once per frame
     void Update () {
 
 
 
         if (scoreCount <= 0) {
             scoreCount = 0;
         }
             
 
         if (scoreCount > highScoreCount) {
             highScoreCount  = scoreCount;    
             SaveHighScore ();
         }
 
         scoreText.text = " " + scoreCount;
         TKOText.text = " " + TKOCount;
         toothCount = scoreCount * .10f;
 
         SaveScore ();
         SaveGoldTeeth ();
 
 
 
 
 }
     void SaveTeeth(float teeth){
 
     }
 
     void SaveHighScore(){
         
         PlayerPrefs.SetInt ("High Score", highScoreCount);
         PlayerPrefs.Save ();
     }
 
     void SaveScore(){
         
         PlayerPrefs.SetInt ("Score", scoreCount);
 
     }
 
     void SaveGoldTeeth(){
         PlayerPrefs.SetFloat ("Tooth Score", toothCount);
         PlayerPrefs.Save ();
     }
 
     void IntializeVariables(){
         if (!PlayerPrefs.HasKey ("Game Initialized")) {
 
             PlayerPrefs.SetInt ("Score", 0);
             PlayerPrefs.SetInt ("High Score", 0);
             PlayerPrefs.SetFloat ("Tooth Score", 0);
 
             PlayerPrefs.SetInt ("Game Initialized", 123);
 
         }
     }
 
 }

Any help is appreciated!

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
0
Best Answer

Answer by Louis-LCM · May 15, 2016 at 12:24 PM

 void Start(){
 highScoreCount= PlayerPrefs.GetInt("HighScore");
 }
 And when u check ur score : 
 if(scoreCount>highScoreCount){
             PlayerPrefs.SetInt("HighScore", scoreCount);
             PlayerPrefs.Save();
         }

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 El-Deiablo · May 15, 2016 at 04:37 PM 0
Share

Thank you so much!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Character selection with scene changing ,Charakter Saving mit Scenenwechsel 0 Answers

Saving highscore for multiple scenes 1 Answer

Saving data with files 1 Answer

How to save home and patrol points along with the enemies? Please 0 Answers

High score resets with score? 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