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 Miyahonmoto · Jun 29, 2020 at 08:26 AM · save datascoreboardbreakout

I want to know how to save high score.

I've made a Brock break game before (it can be said that "long ago..."), and I wanted to save high score in the game and if next player breaks the high score it will be saved until next player breaks the high score, but in my game, the programming isn't working and high score of the game isn't saved. Can someone please tell me how can I solve?

This is my score programming from websites that teaches me a breakout game. using UnityEngine; using UnityEngine.UI;

 public class Score : MonoBehaviour
 {
 
     
     public Text scoreText;
     
     public Text highScoreText;
 
     
     private int score;
 
     
     private int highScore;
 
     
     private string highScoreKey = "highScore";
 
     void Start()
     {
         Initialize();
     }
 
     void Update()
     {
         
         if (highScore < score)
         {
             highScore = score;
         }
 
         
         scoreText.text = score.ToString();
         highScoreText.text = highScore.ToString();
     }
 
     
     private void Initialize()
     {
         
         score = 0;
 
         
         highScore = PlayerPrefs.GetInt(highScoreKey, 0);
     }
 
     
     public void AddPoint(int point)
     {
         score = score + point;
     }
 
     
     public void Save()
     {
         
         PlayerPrefs.SetInt(highScoreKey, highScore);
         PlayerPrefs.Save();
 
         
         Initialize();
     }
 }

and this is the code for adding point

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Destroyer : MonoBehaviour
 {
 
     public GameObject masterObj;
 
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
     private void OnCollisionEnter(Collision collision){
         masterObj.GetComponent<GameMaster>().boxNum--;
         FindObjectOfType<Score>().AddPoint(10);
         Destroy(gameObject);
     }
 }
 



Comment
Add comment · Show 2
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 Hellium · Jun 29, 2020 at 08:43 AM 1
Share

When do you call Save?

avatar image Mrintoxx · Jun 29, 2020 at 10:01 AM 0
Share

You need to call Save() function, maybe into the if condition :

 if (highScore < score)
          {
              highScore = score;
              Save();
          }

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by Batuhan13 · Jun 29, 2020 at 10:22 AM

Hi mate I hope you are well =) You could check this brackeys video Brackey video. For saving or storing some values we use Player prefs too you should check that link too =). I hope these will help you =)

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 Hellium · Jun 29, 2020 at 10:27 AM 1
Share

If you read the code, $$anonymous$$iyahonmoto is already using PlayerPrefs to save and load the highscore....

avatar image Batuhan13 Hellium · Jun 29, 2020 at 11:22 AM 0
Share

Sorry mate my mistake =) I found in that line

PlayerPrefs.SetInt(highScoreKey, highScore);

you have to add (" ") this symbol out of highScoreKey like this

PlayerPrefs.SetInt("highScoreKey", highScore);

avatar image Captain_Pineapple Batuhan13 · Jun 29, 2020 at 11:26 AM 1
Share

no, the current code is correct in that regard because private string highScoreKey = "highScore";

as Hellium pointed out correctly in the comments below the OP the function "Save" is probably just never beeing called upon.

Show more comments

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

132 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

Related Questions

How to revert everything altered by the Binary Formatter? 1 Answer

Networking question. 2 Answers

Score display error 1 Answer

Unity Photon game scoreboard (player properties) any clear tutorials? 2 Answers

Ball angle and movement for Breakout/Arkanoid Clone 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