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 Petrit24 · Jan 12, 2018 at 09:43 AM · score

Saving Highscore Per level

I have a problem with setting the high score per level. I have 5 levels and per level, I want to save a high score. In my game, you have to pick up the collectibles and the time you needed to pick up all the collectibles is your high score but the high score that is made in Level 1 transfers to all the 5 levels. Has anyone a solution to this? Can you fix this with player prefs?

my code for the collectibles:

 if (pickupCount <= 0)
             {
                 player.UpdateTime();
                 if (PlayerPrefs.GetFloat("Highscore") > player.time)
                     PlayerPrefs.SetFloat("Highscore", player.time);
                 levelmanager.LaadVolgende();
             }

and here the script "TimeKeeper". It makes sure that my timer is only rolling when I play the levels and not the win scenes in between

 public class TimeKeeper : MonoBehaviour
 {
     public int level1;
     public int level2;
     public int level3;
     public int level4;
     public int level5;
 
     static TimeKeeper instance = null;
     // Use this for initialization
     void Start()
     {
         if (instance != null)
         {
             Destroy(gameObject);
         }
         else
         {
             DontDestroyOnLoad(gameObject);
             instance = this;
         }
     }
 
     public void UpdateTime(int time, int levelnr)
     {
         switch (levelnr)
         {
             case 2:
                 level1 = time;
                 break;
             case 5:
                 level2 = time;
                 break;
             case 7:
                 level3 = time;
                 break;
             case 9:
                 level4 = time;
                 break;
             case 12:
                 level5 = time;
                 break;
 
         }
 
     }
     public int GeefTijd(int levelnr)
     {
         switch (levelnr)
         {
             case 3:
                 return level1;
 
             case 6:
                 return level2;
 
             case 8:
                 return level3;
 
             case 10:
                 return level4;
 
             case 13:
                 return level5;
             default:
                 return 0;
 
 
         }
     }

I hope you understand my question and can help me with this problem.

translations: LaadVolgende = LoadNext levelnr = level number Geeftijd = GiveTime

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 Hellium · Jan 12, 2018 at 09:49 AM

Simply add a suffix to the PlayerPrefs key :

 int sceneName = SceneManager.GetActiveScene().name ;
 string key = "Highscore_" + sceneName ;
 if (PlayerPrefs.GetFloat( key ) > player.time )
     PlayerPrefs.SetFloat( key, player.time );
 Debug.Log( "Highscore for " + key + " is " + PlayerPrefs.GetFloat( key ) ) ;
Comment
Add comment · Show 8 · 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 Petrit24 · Jan 12, 2018 at 10:25 AM 0
Share

Thanks for your reply! But this code doesn't seem to work for me. $$anonymous$$y levels are called (Level 1, Level 2, etc. So ins$$anonymous$$d of sceneName, I put in Level_1. Can it maybe be fixed with Buildindex? I tried it but am still a noob at program$$anonymous$$g.

avatar image Hellium Petrit24 · Jan 12, 2018 at 10:33 AM 0
Share

If you replace sceneName by Level1, you will have the exact same behaviour as before, so don't do it. If you prefere having the build index, it's very similar:

  int sceneIndex = Scene$$anonymous$$anager.GetActiveScene().buildIndex ;
  string key = "Highscore_" + sceneIndex ;
  if (PlayerPrefs.GetFloat( key ) > player.time )
      PlayerPrefs.SetFloat( key, player.time );
  Debug.Log( "Highscore for " + key + " is " + PlayerPrefs.GetFloat( key ) ) ;
avatar image Petrit24 Hellium · Jan 12, 2018 at 10:47 AM 0
Share

I copied your code and replaced it with what I had but it still doesn't work.. do I have to change something myself or maybe in the editor? Sorry for too many questions

Show more comments
avatar image
0

Answer by Petrit24 · Jan 12, 2018 at 10:28 AM

forgot I got this line of code in another script:

public Text highscoreText; // Use this for initialization void Start() { highscoreText.text = "Your Highscore is : " + (int)PlayerPrefs.GetFloat("Highscore_"); }

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

123 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 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 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 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 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 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

How to reset GUI score after falling down? 4 Answers

For Loops Situation 1 Answer

Score Points based on Animating Png Texture 2 Answers

Score points when player pulls enemy up to certain area ? 0 Answers

Score wont display 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