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 /
avatar image
0
Question by nomiali808 · May 19, 2018 at 05:50 PM · scoresavinghighscores

i want to save high score but my code is not working???,i want to know where i have done mistake with this code . i want to save high score but i i restart the game this will reset??

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class score : MonoBehaviour {

 public Text scoretext;
 public int ballvalue;
 // Use this for initialization
 public int scorevalue;
 public gamecontroller GC;

 public Text highscore;
 void Start () {
     scorevalue = 0;
     highscore.text = PlayerPrefs.GetInt ("HIGH SCORE: \n",0).ToString();
     Updatescore ();
     updatehighscore ();
 }
 void OnTriggerEnter2D()
 {
     scorevalue += ballvalue;
     GC.timeleft = Time.deltaTime +20 ;
 
     Updatescore ();
     timer ();

 
 }
 void OnCollisionEnter2D(Collision2D collision){
 
     if (collision.gameObject.tag == "Bomb") {
     
         scorevalue -= ballvalue ;
     
         Updatescore ();
     }
 }
 
 // Update is called once per frame
 void Updatescore () {
 updatehighscore ();
     scoretext.text = "score:\n" + scorevalue;

 
     }
 void updatehighscore() {
 
     if (scorevalue > PlayerPrefs.GetInt ("HIGH score:\n", 0)) 
     {

         PlayerPrefs.SetInt ("HIGH SCORE:\n", scorevalue);
         highscore.text = scorevalue.ToString ();
     }
     }
 void timer()
 {
     GC.timertext.text = "Time Left:\n" + Mathf.RoundToInt (GC.timeleft);
 }

} ,

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Happeloy · May 19, 2018 at 05:54 PM

You need to call

 PlayerPrefs.Save();

after your call PlayerPrefs.SetInt(); to actually save the score.

Edit: you also need to be consistent when getting/setting the values, it's case sensitive. You are using three different strings here, "HIGH SCORE: \n", "HIGH score:\n" and "HIGH SCORE:\n", they need to be identical to all point to the same thing. And you don't need to put the \n in there.

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

85 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

Related Questions

Saving score and applying highscore (C#) 2 Answers

Recognising high score when updated with a graphic 0 Answers

Letter Ranking System, Checking Higher Rank 1 Answer

Saving Highscore 2 Answers

Loading a score on a scene 2 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