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 kwm · Aug 15, 2015 at 05:10 PM · c#scoreunity 4.6highscoreinfinite runner

Why doesn't my highscore script work?

I'm making a little endless runner game where the camera follows a moving character. I managed to make the score gui work but for some reason my highscore gui doesn't keep track of the highest score and just displays 0 all the time. I'm new to coding but have some experience with how unity works, I feel like i'm just overlooking something very simple but haven't been able to find answers online. Here is my code for managing the score/highscore:

 using UnityEngine;
 using System.Collections;
 
 public class NewScore : MonoBehaviour {
     
     static int score = 0;
     static int highScore = 0;
 static public void AddPoint() {
 if(score > highScore) {
             highScore = score;
         }
     }
 void Start() {
 score = 0;
         highScore = PlayerPrefs.GetInt("highScore", 0);
     }
     
     void OnDisable() {
 PlayerPrefs.SetInt("highScore", highScore);
         PlayerPrefs.SetInt ("score", score);
     }
     
     void Update () {
         guiText.text = "Score: " + score + "\nHigh Score: " + highScore;
         PlayerPrefs.SetInt ("highScore", highScore);
     }
     
     public void IncreaseScore(float amount)
     {
         score += (int)amount;
     }
 }





Comment
Add comment · Show 1
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 kwm · Aug 16, 2015 at 12:20 AM 0
Share

the "`static public void AddPoint()`" was copied from another game's code I have which had a score and highscore so I tried using it for my game. In the original game, there was another script which had this: "

 void OnTriggerEnter2D(Collider2D collider) {
         if(collider.tag == "Player") {
             Score.AddPoint();

" And this ^ was attached to invisible prefabs (in the game i got the code from, not my current infinite runner) that increased the score when triggered by the player. The IncreaseScore part was added so that powerups/powerdowns can affect the score. This script is attached to the powerups/powerdowns: "

 using UnityEngine;
 using System.Collections;
 
 public class PointScript : $$anonymous$$onoBehaviour {
     
     NewScore hud;
     
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Player") {
             hud = GameObject.Find ("NewScore").GetComponent<NewScore>();
             hud.IncreaseScore (10);
             Destroy (this.gameObject);
         }
     }
     
 } 

" Are these two (IncreaseScore & AddPoint) parts of my script conflicting with one another somehow? Or does my problem lie elsewhere?

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by kwm · Aug 16, 2015 at 12:25 AM

Oh wow... I just answered my own question lol... I cut

 if(score > highScore) {
              highScore = score;

from the AddPoint function and pasted it into the IncreaseScore function and now my highscore works properly! thanks everyone for their help though, helped me look more closely at my code.

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
2

Answer by 1Piotrek1 · Aug 15, 2015 at 09:06 PM

I think that it's because GetInt taking only one argument - value name. If it isn't solution you should use Debug.Log to see what's wrong.

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 kwm · Aug 16, 2015 at 12:26 AM 0
Share

Thanks for your help!

avatar image
1

Answer by GuntranHoet · Aug 15, 2015 at 09:14 PM

I would put this line: PlayerPrefs.SetInt ("highScore", highScore); After the 'score += (int)amount' in IncreaseScore. That way your save data of the high score will only be saved when it's increasing. Not every tick, like you're doing now ;)

To help your problem: Add 'using UnityEngine.UI' on top of you file. Next place a 'Text' Variable under the line static int highscore. (example: 'public Text ScoreText;' )

Then you can drag the text object from the editor into the text slot on your gameobject with this script :)

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 kwm · Aug 16, 2015 at 12:27 AM 0
Share

Thanks for the help and tips! I attached this script to the text object though so i can't drag it into itself. Got my highscore function working though.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Implement own highscore script into existing FB SDK example 1 Answer

whats wrong with my high score code? 1 Answer

High score value remains constant in game over scene, even if player has scored higher. 1 Answer

Scoring System Unity 6.4 C# 2D Game 1 Answer

I'm trying to set a high score but I can't display it in another 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