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 emek12 · Dec 14, 2013 at 04:51 AM · gameobjectsavescoresave scene

How to save High Scores

Hey guys Im stumped on how to save high scores. After my player gets killed and he has the highest score I want his score to be saved. I know I use PlayerPref class but im not sure how to use this.

 static var score = 0; //current score
 var totalScore = 0; //combined total of players score
 var highscore = 0; //highest score player has gotten
 
 
 var scoretext : GUIText;
 var money : AudioClip;
 
 function Update()
 {
 
     scoretext.text = score + ""; //display score
     
     if(score > highscore && TurnGameOver.dead2 == true) //when player dies set highscore = to that score
     {
         highscore = score;
         PlayerPrefs.SetInt("High Score", highscore);
         
         Debug.Log("High Score is " + highscore );
     
     }    
     
 
 
 
 }
 
 function OnTriggerEnter(other : Collider)
 {
      //add to players score if he collects a gem
  
     if(other.gameObject.name == "GreenGem" || other.gameObject.name == "BlueGem" || other.gameObject.name == "RedGem" || 
     
     other.gameObject.name == "GreenGem(Clone)" || other.gameObject.name == "BlueGem(Clone)" || other.gameObject.name == "RedGem(Clone)")
     {
         
         score += 10;
         
         Debug.Log("Your score is " + score);
         Destroy(other.gameObject); //Destroys Gem after player collects it
 
     
     }
     
 }
Comment
Add comment · Show 4
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 AlucardJay · Dec 14, 2013 at 05:14 AM 0
Share

Have you read the Documentation? Or one of the many other questions here on using PlayerPrefs? Or done any searching?

You havn't typecast your score variables so I'm going to assume they are integers :

  • Save : http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.SetInt.html

  • Load : http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.GetInt.html

avatar image MrProfessorTroll · Dec 14, 2013 at 05:25 AM 0
Share

Well, you are saving it. What is your problem? Are you getting any errors? Is the score not appearing anymore?

avatar image RandomCharacters · Feb 28, 2014 at 12:44 AM 2
Share

Why with the attitude alucardj? Geez. Don't you think people have looked and searched before? Either help the guy out or STFU. There si too much unity superiority here. I have the same questions as this guy does and it gets old reading dozens of these comments.

avatar image AlucardJay · Feb 28, 2014 at 02:59 AM 0
Share

@wheretheidivides : Check the links in my comments, then pull your head in. You don't know good help when you're getting it.

1 Reply

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

Answer by Seth-Bergman · Dec 14, 2013 at 06:02 AM

you want to save the high score so it still shows up after closing and relaunching the game, yes? you have done the first part right, saving the score.. but in order to have it show up next time, you need to retrieve it first.. use the Start function:

 function Start(){
 highscore = PlayerPrefs.GetInt("High Score");
 }

simple as that!

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 emek12 · Feb 28, 2014 at 12:48 AM 1
Share

Im a little late on the reply but thanks a ton man, your the best xD

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

21 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

Related Questions

OnLevelWasLoaded script problem. 1 Answer

Problem with High Score System using PlayerPrefs [C#] 1 Answer

How to save my settings 1 Answer

How to save a gameObjects on a prefab and variables with a script? 1 Answer

How I can drag objects(songs) into the game(at runtime) and save it? 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