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 kjb12312 · Mar 23, 2013 at 05:07 AM · timerhighscores

How do I create a high score system in my game?

My game has you driving around avoiding enemy cars for as long as you can. I want my game to have a score saving system that shows how long you survived. I already have the timer implemented in. The timer script I am using is as follows.

 var time : int = 1;
 
 function Start () {
     while(time != 0) {
         yield WaitForSeconds(1);
         time = time + 1;
     }
 }
 
 function OnGUI () {
     GUI.Box(Rect(0, 0, 100, 30),"Time: " + time);
 }

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 Fattie · Mar 23, 2013 at 06:25 AM 0
Share

FTR consider Invoke() or InvokeRepeating() for simple timers in Unity

avatar image Fattie · Mar 23, 2013 at 03:07 PM 0
Share

learn about Player.Prefs

avatar image justinl · Mar 23, 2013 at 08:31 PM 0
Share

Player.Prefs is also a really good alternative. If it was me I'd probably use that, since you're only saving such small data.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by justinl · Mar 23, 2013 at 05:56 AM

simply create another variable called var highScoreTime : int; and then when you die or lose, set highScoreTime = time. Then when the game restarts, don't overwrite the highScoreTime variable. However, this will be lost as soon as you stop playing the game. If you want it to save it permanently you'll need to do some extra work such as implementing this XML Save/Load package: http://wiki.unity3d.com/index.php?title=Save_and_Load_from_XML or just google "Save/Load in Unity" and you'll see plenty of various solutions with different degrees of difficulty.

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 kjb12312 · Mar 23, 2013 at 08:02 AM 0
Share

Ok so i have the timer script on my car and have tagged the enemy cars with Finish. I cant figure out how to set the high score time. I'm new to coding so if the solution is really obvious I'm sorry. Here is my new code

 var time : int = 1;
 var highScoreTime : int;
 
 function Start () {
     while(time != 0) {
         yield WaitForSeconds(1);
         time = time + 1;
     }
 }
 
 function OnGUI () {
     GUI.Box(Rect(0, 0, 100, 30),"Time: " + time);
     GUI.Box(Rect(1250, 0, 100, 30),"High Score: "  );
 }
 
 function OnCollisionEnter(collision : Collision)
 { if (collision.gameObject.tag == "Finish") {set highScoreTime = time;
     }
 }
avatar image justinl · Mar 23, 2013 at 08:14 AM 0
Share

GUI.Box(Rect(1250, 0, 100, 30),"High Score: " + highScoreTime ); highScoreTime = time; //does not require the word "set". What specifically is not working?

avatar image kjb12312 · Mar 23, 2013 at 09:36 AM 0
Share

Thanks for your help justinl. But I have one more thing I wish to know. I have my game set so that if you get hit by the enemy cars a "you lose" screen comes up and then you go back to the main menu. Once this happens my score disappears . the X$$anonymous$$L thingy doesn't work as it relies on clicking Save. If you know any alternate ways that would be ideal. if you cant I'll just look at the script and see if i can get ti to trigger on level change or something.

avatar image justinl · Mar 23, 2013 at 10:13 AM 0
Share

It really depends on how you're "restarting" your game. If you're destroying your player controller car and re-creating one then for sure the score will be lost. If you don't $$anonymous$$d having the variable saved only for the duration of the game, then what I'd do is create a game manager object in your scene. Just a random dummy object and you assign a script to it that will keep track of things like highscore, or how many lives the player has. You will not destroy this object and it will be persistent throughout the life of your program. That's where you can store your highscore. If you want to try using the X$$anonymous$$L thing, then it's really easy because you say it relies on clicking Save, but you can simply do the "click" of the save button in code ins$$anonymous$$d of an actual mouse button click. Just copy and paste what the X$$anonymous$$L example has inside of the if (GUI.Button(_Save,"Save")) { //copy and paste this stuff into the place where you reset the game such as inside the collision.gameObject.tag == "Finish" brackets }

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

12 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

Related Questions

using player time as highscore 1 Answer

Timer highscore trouble 1 Answer

Unity ignoring if statement for high score 2 Answers

How do I make a Highscore for my timer? 1 Answer

"Best Time"/high score help… 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