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 gimoj · Jul 10, 2014 at 06:10 PM · c#sceneplayerprefsscorehighscore

I'm trying to set a high score but I can't display it in another scene?

I tried using this code to save a single high score but I'm having trouble using it.

This is for storing and displaying the score on the stage:

 void Update () {

     horizontalRatio = Screen.width / BASE_WIDTH;
     verticalRatio = Screen.height / BASE_HEIGHT;
     
     guiText.text = "Score: " + score;
     guiText.fontSize = Mathf.Min(BASE_HEIGHT,BASE_WIDTH)/FSize;

 }

 public static void StoreHighscore(int newHighscore)
 {
     int oldHighscore = PlayerPrefs.GetInt("score", 0);
     if(newHighscore > oldHighscore)
         PlayerPrefs.SetInt("score", newHighscore);
         PlayerPrefs.Save();
 }    

And this is my script for the display of the high score in the high score scene:

 void Start () {

 }
 
 // Update is called once per frame
 void Update () {
     
     horizontalRatio = Screen.width / BASE_WIDTH;
     verticalRatio = Screen.height / BASE_HEIGHT;
     
     //        var scoreposx = horizontalRatio / ScoreX;
     //        var scoreposy = verticalRatio / ScoreY;
     
     guiText.text = "Score: " +PlayerPrefs.GetInt("newHighscore");
     guiText.fontSize = Mathf.Min(BASE_HEIGHT,BASE_WIDTH)/FSize;
     
 }

There aren't any errors showing up but I can't seem to make the high score show. I'm not even sure if the high score was saved.. I know this is all very basic and I've tried researching on the net and even youtube but I can't seem to find a way to make this work. Can anyone help me?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by zharik86 · Jul 10, 2014 at 06:22 PM

You save your score in variable by name "score":

  PlayerPrefs.SetInt("score", newHighscore);

But, you show on screen variable by name "newHighscore":

  guiText.text = "Score: " +PlayerPrefs.GetInt("newHighscore");  //value 0 default

Change this line how:

  guiText.text = "Score: " +PlayerPrefs.GetInt("score");

That will work.

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 gimoj · Jul 10, 2014 at 06:45 PM 0
Share

I tried it now bu it's still the same. In the first code I store the active score in score that's why:

guiText.text = "Score: " + score;

Do I have to separate it from:

int oldHighscore = PlayerPrefs.GetInt("score", 0);

Like maybe turn it into:

int oldHighscore = PlayerPrefs.GetInt("highscore", 0);

avatar image BlackPanda · Jul 10, 2014 at 07:22 PM 0
Share

PlayerPrefs.GetInt() only accepts one string value as its parameter. So use

 int oldHighScore = PlayerPrefs.GetInt("score");
avatar image gimoj · Jul 10, 2014 at 07:45 PM 0
Share

It also doesn't work.. Do I have to call the script or something for the display scene?

avatar image
0

Answer by Tehnique · Jul 10, 2014 at 06:24 PM

You set the key as "score" and then retrieve it as "newHighscore". Replace line 14 with guiText.text = "Score: " +PlayerPrefs.GetInt("score");

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 gimoj · Jul 10, 2014 at 06:51 PM 0
Share

I tried that but it's the same. Do I have to call the other script in the display to make it work?

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

22 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

Related Questions

PlayerPrefs.GetInt does not show up 1 Answer

How to write If the player don't play the game for 24 hours, the game is over. 1 Answer

How to save a high score 2 Answers

How to stop objects you picked up from reappearing when you go to another level and return 1 Answer

Sorting Error 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