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 beginningsir · Sep 05, 2014 at 11:19 AM · timejavahighscore

HighScore not updating when the game time is up...

Hi. I have two different scripts in the game, one for the countdown time (Timer) and one for the score (Score). And my point is to set a new HighScore when the time is up. However, the highscore doesn't seem to be working when I call it on debug.log, it always stays as 0. What am I doing wrong? Here is part of the Score script :

 static var score : int = 00;
 static var highscore : int = 00;
 static var highScoreKey = "HighScore";
 
 function Start () {
 
 highscore = PlayerPrefs.GetInt(highScoreKey,0);
 }
 
 function SetNewHighScore () {
     if (Timer.seconds==0) {
         if (score > highscore) {
             PlayerPrefs.SetInt(highScoreKey, score);
             PlayerPrefs.Save();
         }
     }
 }

And here is part of the Timer script where I call the saved data:

 function OnGUI () {
 
 textTime = String.Format("{00:00}", seconds);
 
     GUI.skin = theTimerSkin;
     GUI.Label (new Rect (Screen.width/2,18,100,100), textTime);
 
     if(seconds <= 0) {
     seconds=0;
     **Debug.Log(Score.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 Tanshaydar · Sep 05, 2014 at 11:42 AM 0
Share

Why are you using static values?

avatar image GameVortex · Sep 05, 2014 at 11:47 AM 0
Share

Well, you never call your function SetNewHighscore.

avatar image beginningsir · Sep 06, 2014 at 05:11 AM 0
Share

@Tanshaydar: I use static values so I can access them from other parts. @GameVortex: Could you please develop on why I can't call it that way, I thought we could call a function whatever we want...

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by rutter · Sep 06, 2014 at 05:47 AM

This bit:

     if (score > highscore) {
         PlayerPrefs.SetInt(highScoreKey, score);
         PlayerPrefs.Save();
     }

You're updating the high score that's saved on disk, but you're not updating the variable highscore.

You need:

     if (score > highscore) {
         highscore = score;
         PlayerPrefs.SetInt(highScoreKey, score);
         PlayerPrefs.Save();
     }

You're not showing anything that calls SetNewHighScore; are you sure that's being called?

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 beginningsir · Sep 06, 2014 at 06:01 AM 0
Share

@Rutter: Genius! Thank you very much, this solved the problem. And, yes I wasn't calling the function earlier but I started calling the function when the time is up and now it's working fine!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

how to find time of a button pressed 2 Answers

Timer help please 1 Answer

How do you save timer with PlayerPrefs? 1 Answer

How can I use time to reset a switch statement? 1 Answer

Time formatting 3 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