Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Gusic · Jul 26, 2015 at 10:28 PM · c#scorehighscores

Saving score and applying highscore (C#)

Hi,

I have a score script which works great but the thing is i want to implement highscore, which means i have to save my score. The thing is i have looked online but there scripts are so different to mine. Do you think i can implement a highscore to my game so when the scene restarts the highscore stays.

CODE

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class Player : MonoBehaviour
 {
     public Text countText;
     private int count;
     public Text scoreText;
     private int scount;
     public AudioClip coin;
     public AudioClip enemy;
 
     
     
     void Start ()
     {
 
         count = 0;
         SetCountText ();
         scount = 0;
         SetScoreText ();
 
         
     }
     
     void update () {
     
     
 
     
     
     }
     
     void OnTriggerEnter2D(Collider2D col) 
     {
         if(col.gameObject.tag == "Coin")
         {
             col.gameObject.SetActive (false);
             AudioSource.PlayClipAtPoint(coin, transform.position);
             count = count + 1;
             SetCountText ();
             scount = scount + 1;
             SetScoreText ();
         } else if (col.gameObject.tag == "Enemy")
         {
             AudioSource.PlayClipAtPoint(enemy, transform.position);
             Destroy(gameObject);
             scoreText.gameObject.SetActive (true);
         }
     }
     
     void SetCountText ()
     {
         countText.text = " " + count.ToString ();
 
         
     }
     void SetScoreText ()
     {
         scoreText.text = " " + scount.ToString ();
         
         
     }
 }

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 jprocha101 · Jul 26, 2015 at 10:40 PM

You can use player prefs for that. In your Start method load the high score. In your Update method once the score is greater than the high score then save the new score as the high score.

http://docs.unity3d.com/ScriptReference/PlayerPrefs.html

Comment
Add comment · Show 2 · 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 Hexer · Jul 26, 2015 at 10:49 PM 0
Share

Unity Reference explains it really poorly imo

avatar image jprocha101 · Jul 26, 2015 at 11:14 PM 0
Share

Under the functions section there is an example of each method. Drilling down into each link, reading the description and exa$$anonymous$$ing the example is a great way to learn :)

avatar image
0

Answer by Hexer · Jul 26, 2015 at 10:47 PM

Something like this?

    int Highscore;
         
         void Start(){
         //Each time the scene starts it will get the Highscore that was stored in the playerPrefs.
         Highscore = PlayerPrefs.GetInt("Highscore",0);
         }
         
         void Update(){
 //if the variable Score from another script is lower or equal to Highscore, 
 //set the HighScore tobe equal to the Score.
 //PlayerPref.SetInt
         if(ScoreScript.Score > Highscore){
         Highscore = ScoreScript.Score;
         PlayerPref.SetInt("Highscore", Highscore);
         }
         }

If the highscore is higher or equal to the Score than the Highscore is equal to the score. It will then set the int in the PlayerPref. This would safe the highscore.

Here the Scorescript stands for the name of the script where you access Score. Score is a static public int in that Scorescript. (I didn't know how you call it in your script so I just called it Score to be direct)

Comment
Add comment · Show 2 · 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 jprocha101 · Jul 26, 2015 at 11:18 PM 0
Share

Don't you have the IF condition reversed? You would want to set the high score if your ScoreScript.Score > Highscore

avatar image Hexer · Jul 26, 2015 at 11:21 PM 1
Share

Well you can put it in a custom function of its own.

 void Custom(){
    if(ScoreScript.Score > Highscore){
          Highscore = ScoreScript.Score;
          PlayerPref.SetInt("Highscore", Highscore);
          }
 }

And call it when the game ends.

 Custom();

EDIT : UHH$$anonymous$$ yes you are right (misread reversed for reserved..) . I changed it, my bad. (it's 01:25am in my country and Im still working on my game and answering questions xD, gotta nap soon I guess.

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

23 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

Related Questions

Creating score based on time with PlayerPrefs 1 Answer

whats wrong with my high score code? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Highscore not working 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