- Home /
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;
}
}
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?
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.
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.
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 :)
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
Follow this Question
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