Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 fatmanspineapple · Jan 12, 2015 at 01:31 PM · playerprefs

Save and Retrieve Score not working!

Dear all, I have been encountering a very irritating problem when creating my first app. I am trying to use the playerpref to save my score, but when it restarts, the highscore is reset to 0. I am an absolute beginner, so please treat me like a dunce. Ill paste the 2 scripts I have for scoring a point on trigger, and the other is the general score system. I have tried this on my phone, and whenever I score a point and update the score, the highscore is 0. Many thanks for the help :) from fatmanspineapple For the score point using UnityEngine; using System.Collections;

 public class ScorePoint : MonoBehaviour {
 
     void OnTriggerEnter2D(Collider2D collider){
         if(collider.tag == "Player"){
             ScoreSystem.AddPoint();
             gameObject.SetActive(false);
         }
 
 
 }
     }

For the score system

 using UnityEngine;
 using System.Collections;
 
 public class ScoreSystem : MonoBehaviour {
 
     static int score = 0;
     static int highScore = 0;
     static ScoreSystem instance;
 
     static public void AddPoint(){
                 score++;
 
                 if (score > highScore) {
                         highScore = score;
                 }
         }
 
 
     void Start() {
         PlayerPrefs.GetInt("highScore", 0);
 
 
     }
     void OnDestroy(){
         PlayerPrefs.SetInt ("highScore", highScore);
         score = 0;
 
           }
                          
     void Update () {
         guiText.text = "S:" + score +  "\nHS:" + highScore;
     
     }
 }

Comment
Add comment · Show 3
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 HarshadK · Jan 12, 2015 at 01:31 PM 0
Share

You need to save your PlayerPrefs in order for the changes to be made to the disc. Use PlayerPrefs.Save();

  void OnDestroy(){
          PlayerPrefs.SetInt ("highScore", highScore);
          // This line is important
          PlayerPrefs.Save();
         score = 0;
  
            }

avatar image fatmanspineapple · Jan 13, 2015 at 06:34 PM 0
Share

I was also wondering whether there was a code I could use to display this highscore on a different screen? After the game object collides with a killing game object, a screen is loaded and this highscore is displayed.

avatar image fatmanspineapple · Jan 14, 2015 at 05:43 PM 0
Share

Dear Harshad, I tried this on my phone. The highscore saves but when I close the app and then reopen it, the high score resets to 0 again. i have tried everything and Im not sure what to do now

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by gjf · Jan 15, 2015 at 01:35 PM

you never initialize highScore from player prefs - line 20:

 PlayerPrefs.GetInt("highScore", 0);

should be

 highScore = PlayerPrefs.GetInt("highScore", 0);


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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

PlayerPrefs Problem crashing with SetBool 2 Answers

How to use PlayerPrefs? 2 Answers

Save level 3 Answers

How to save resources configuration file for unity3d? 0 Answers

Saving Location in Unity 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