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 laszlar · Apr 25, 2016 at 10:54 PM · playerprefshighscores

PlayerPrefs isn't saving between Scenes

I've tried looking at a bunch of answers on here, but still having bad luck. I'm using PlayerPrefs to save a high score then switch scenes and show that high score. It actually works (sort of) - in play scene it stores the high score, then in game over scene it shows it. You have the option to restart from game over scene (SceneManager.LoadLevel) and it works, but after a few times it just doesn't show the same high score. It's almost like it resets itself. See play scene:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class ScoreTracker : MonoBehaviour {
 
     PlayerMovement playerMove;
     Text text;
     public int points = 0;
     public int highScore;
 
     void Start()
     {
         playerMove = GameObject.Find("Player").GetComponent<PlayerMovement>();
         text = gameObject.GetComponent<Text>();
     }
     
     // Update is called once per frame
     void Update ()
     {
        points = playerMove.points;
        text.text = "" + points;
        if (points > highScore)
         { 
             highScore = points;
             PlayerPrefs.SetInt("High Score", highScore);
             PlayerPrefs.Save();
         }
     }
 }

and Game over scene script:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class HighScoreTracker : MonoBehaviour
 {
     Text text;
     public int currentScore;
 
     void Start ()
     {
         text = GetComponent<Text>();
     }
     
     // Update is called once per frame
     void Update ()
     {
         currentScore = PlayerPrefs.GetInt("High Score");
         text.text = "" + currentScore;
     }
 }

And I have tried running Debug.Log after every playerpref, but it stops showing what it's saving... because it's actually working? Any ideas from anyone?

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

Answer by Eno-Khaon · Apr 25, 2016 at 11:26 PM

It looks like you're never assigning your saved high score to your highScore value.

When you create the public variable highScore, it's being initialized to 0. The first place you use it after that is when you check whether the current score is higher than it.

 public int highScore;
 // ...
 if (points > highScore)

Because you're not also loading the high score into that variable, you're overwriting the old one with every fresh game session.

Comment
Add comment · Show 4 · 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 laszlar · Apr 26, 2016 at 03:22 AM 0
Share

@$$anonymous$$o $$anonymous$$haon It seems as if I'm missing something here still. I know what you mean, just not sure how to implement. I wrote this, but it just made it even worse.

 public class ScoreTracker : $$anonymous$$onoBehaviour
 {
 
     Player$$anonymous$$ovement player$$anonymous$$ove;
     Text text;
     public int points = 0;
     public int highScore;
 
     void Awake ()
     {
         PlayerPrefs.SetInt("High Score", highScore);
     }
 
 
     void Start()
     {
         player$$anonymous$$ove = GameObject.Find("Player").GetComponent<Player$$anonymous$$ovement>();
         text = gameObject.GetComponent<Text>();
     }
     
     // Update is called once per frame
     void Update ()
     {
        points = player$$anonymous$$ove.points;
        text.text = "" + points;
        if (points > PlayerPrefs.GetInt("High Score"))
         {
             points = highScore;
             PlayerPrefs.SetInt("HighScore", highScore);
             PlayerPrefs.Save();
         }
     }
 }

Am I on to something here? Or still off completely?

avatar image Eno-Khaon laszlar · Apr 26, 2016 at 03:39 AM 0
Share
  void Awake ()
  {
      PlayerPrefs.SetInt("High Score", highScore);
  }

Close, but also the exact opposite of the intended outcome. Try this ins$$anonymous$$d:

  void Awake ()
  {
      highScore = PlayerPrefs.GetInt("High Score", 0);
  }
avatar image laszlar Eno-Khaon · Apr 26, 2016 at 03:55 AM 0
Share

I see what you mean and changed it (Thank you!), but it's returning a zero value in the game over scene oddly.

Show more comments

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

43 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

Related Questions

One PlayerPref not saving 1 Answer

Taking high score from anther script and then displaying it 2 Answers

How do I save highscore (Very Simple) 3 Answers

Score not resetting 3 Answers

PlayerPrefs not working with android 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