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 kriyor · Feb 11, 2018 at 12:04 AM · unity 5unity 2dplayerprefsscore systemhighscore

High-score Saving Issue

I want to implement a high score system in my dodge game, but I can't seem to make it work. Currently, I have a score, which starts from 500,000 and gets down 150 points per seconds. This works, although when I die it still goes on, which I need to fix. I have a second kmHighscore Text, and I want it to show the highscore. Right now it shows the exact score on which I died(for example 450,875), but when I restart the game I can't seem to make it be saved. I tried with PlayerPrefs, but I am not sure how to apply it in my code. I want it to represent the lowest, in this case as it starts from 500,000, score ever achieved. Just to note: Both kmScore and kmHighscore are just numbers, I don't have text before the score or the highscore in the Unity window. Any help will be appreciated!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class GameOver : MonoBehaviour {
 
     public GameObject gameOverScreen;
     public Text kmScore;
     public Text kmHighscore;
     float savedScore;
     bool gameOver;
     private float score = 500000;
 
     void Start () {
         FindObjectOfType<PlayerController>().OnPlayerDeath += OnGameOver;          
     }
 
     public void Update () {
 
         kmScore.text = GetScore().ToString("F0");
 
         if (gameOver)
         {
             if (Input.GetKeyDown (KeyCode.Space))
             {
                 SceneManager.LoadScene(1);
             }
         }
     }
 
 
     float GetScore()
     {
         return score - (float)Time.timeSinceLevelLoad * 150;
     }
 
 
     void OnGameOver()
     {
 
         gameOverScreen.SetActive (true);
         kmHighscore.text = GetScore().ToString("F0");
 
     }
 }
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
1

Answer by Cuttlas-U · Feb 11, 2018 at 02:46 PM

hi; there are some things need to be done to fix this ;

first of all calculating the High Score should be only when its not game over so u should change this part in the update method :

then u need to save the score when u die and load it when the game starts again ;

to save the score u need to have another variable to store the last highscore cause u dont access that function when u die;

and at last u need to check if the current score is not more then your last saver high score ;

so it all changes like this

 CurrentHighScore = 0; void Start() { FindObjectOfType().OnPlayerDeath += OnGameOver;
 
          if ( PlayerPrefs.HasKey("HighSScore"))
          {
             CurrentHighScore = PlayerPrefs.GetFloat("HighSScore");
             kmScore.text = CurrentHighScore.ToString("F0");
             
          }
      }
      float CurrentScore;
      public void Update()
      {
        
          if (gameOver)
          {
              if (Input.GetKeyDown(KeyCode.Space))
              {
                  if (CurrentScore > CurrentHighScore)
                  {
                      PlayerPrefs.SetFloat("HighSScore", HighestScore);
                  }
                  SceneManager.LoadScene(1);
              }
          }
          else
          {
              CurrentScore = GetScore();
              if ( CurrentScore > CurrentHighScore)
              { 
              kmScore.text = GetScore().ToString("F0");
              }
          }
      }
      float HighestScore;
      float GetScore()
      {
          HighestScore = score - (float)Time.timeSinceLevelLoad * 150;
          return HighestScore;
      }


i did this really quick so they mayebe some problem tell me about them if u see;

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 codeblue00 · Feb 11, 2018 at 06:45 AM

This is a good tutorial for easily implementing a save/load function with JSON. if you want to use player prefs I'm pretty sure this tutorial covers saving/loading with that option.

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 kriyor · Feb 11, 2018 at 01:57 PM 0
Share

Not really helpful. I have used PlayerPrefs. the problem is that I can't implement it with my code.

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

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

Highscore not working 0 Answers

Modification to Unity2D Roguelike 1 Answer

Player character jumping when I wish to only initiate pause - UI Button element. 1 Answer

Prefab Direction - shooting 2 Answers

2D Collision Best Practice 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