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 /
  • Help Room /
avatar image
0
Question by wwewef · Oct 26, 2015 at 12:30 PM · 2d2d gamescore systemhighscoreshighscore

Code check. Is my Highscore saving code OK?

Hi guys, working on a class project. Its a 2D shooter top down scroller. Right now I got the game working with some scaling problems but working as intended. Now I aim to store highscores.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class ScoreScript : MonoBehaviour {
 
     public Text scoreText;
 
     public int score = 0;
 
     public void addScore(int amount) {
         score += amount;
         scoreText.text = "Puntuacion:\n " + score;
         StoreHighscore (score);
     }
     void StoreHighscore(int newHighscore)
     {
         int oldHighscore = PlayerPrefs.GetInt("highscore", 0);    
         if(newHighscore > oldHighscore)
             PlayerPrefs.SetInt("highscore", newHighscore);
     }
 }
 

Any tip about where to store it? How to store it the correct way, etc... Im not new to Unity but I havent done much of storing data. Thanks!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Landern · Oct 26, 2015 at 02:29 PM

Well you're already using PlayerPrefs do you have a reason not to use that to store it? You can store it with a simple Save method call after the set if the idea is local storage of the highscore.

  if(newHighscore > oldHighscore) {
              PlayerPrefs.SetInt("highscore", newHighscore);
              PlayerPrefs.Save();
   }

Click the PlayerPrefs link for more information about various platforms and PlayerPrefs storage.

After it's saved you can pull the player preference for "highscore" just as you did already in your code above.

Comment
Add comment · 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
0

Answer by MadDevil · Oct 26, 2015 at 02:34 PM

@wwewef

try this,

 void StoreHighscore(int newHighscore)
     {
         if(PlayerPrefs.HasKey("Example"))
         {
             int i = PlayerPrefs.GetInt("Example");
 
             if(newHighscore > i)
             {
                 PlayerPrefs.SetInt("Example", newHighscore);
                 PlayerPrefs.Save();
             }
 
         }
         else
         {
             PlayerPrefs.SetInt("Example", newHighscore);
             PlayerPrefs.Save();
         }
     }
Comment
Add comment · 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
0

Answer by Statement · Oct 26, 2015 at 03:06 PM

It looks fine to me.

You don't have to call Save() every time you make a change, and the documentation states you should avoid calling it during gameplay. Save is called automatically by Unity, but you may want to periodically autosave in case the UnityPlayer/game/computer/device crashes unexpectedly, or when transitioning between scenes etc.

PlayerPrefs is conceptually meant to store Preferences (Hence "PlayerPrefs"). It's not that robust to handle large amounts of information that typically goes with a save game but is fine for your use case.

When I say it's not that robust, I can give you a concrete example. I was trying to be clever and making local savegames on a webplayer build using playerprefs. I had a chunk of binary data which I base64 encoded into a string. Then I could save the entire game like PlayerPrefs.SetString("Savegame", base64SaveGame);. This worked fine for a while, until the size of the save game reached about 250 kB. After that it stopped saving the game.

Note that anyone can manually edit the highscore "preference" on the system. Like on windows, it'll be stored as plain text in the Registry. Though if you are just toying around and don't really need any more complicated system for keeping track of your game state, then PlayerPrefs can be used this way.

Comment
Add comment · 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

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

High Score Saving and Displaying on another scene 0 Answers

Hey i am trying to add a time score system and time high score system need help. 0 Answers

Walking around 2D planets? 1 Answer

Player follows mouse, but I need only Y 1 Answer

I need to deactivate an animation trigger 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