Highscore not working
Hello,
I am making a 2d catch game where you have to catch falling objects. I am trying to make a HighScore system, but I am really stuck right now..
This is my score script:
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class Score : MonoBehaviour {
public static Score HS;
public Text scoreText;
public Text HighScore;
public int krolschValue;
private int score;
int highScore;
void start () {
HS = this;
score = 0;
UpdateScore ();
highScore = PlayerPrefs.GetInt("HighScore1",0);
}
void OnTriggerEnter2D () {
score += krolschValue;
UpdateScore ();
}
void UpdateScore () {
scoreText.text = "X " + score;
HighScore.text = "HighScore: " + highScore;
}
public void CheckHighScore()
{
if(score > highScore)
{
Debug.Log ("Saving Score");
PlayerPrefs.SetInt("HighScore1",score);
}
}
}
And this is my LifeManager script (where I am using the function of when I am out of lifes, in the void Update):
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class LifeManager : MonoBehaviour {
public int startingLives;
private int lifeCounter;
private Text theText;
public GameObject GameOverScreen;
public GameManager Spawner;
// Use this for initialization
void Start () {
theText = GetComponent<Text>();
lifeCounter = startingLives;
Spawner = FindObjectOfType<GameManager>();
}
// Update is called once per frame
void Update () {
if(lifeCounter <= 0)
{
GameOverScreen.SetActive(true);
Spawner.gameObject.SetActive(false);
Score.HS.CheckHighScore();
}
theText.text = "X " + lifeCounter;
}
public void GiveLife()
{
lifeCounter++;
}
public void TakeLife()
{
lifeCounter--;
}
}
My score is working perfectly, but when I am out of lifes and the GameOver GUI comes over, where the HighScore text is, it always just shows 0.
Please help
Bram
addition:
When I don't catch anything, so I have 0 points, the Highscores text shows no value.
Should you call UpdateScore ()
here
if(lifeCounter <= 0)
{
GameOverScreen.SetActive(true);
Spawner.gameObject.SetActive(false);
Score.HS.CheckHighScore();
}
too?
$$anonymous$$y console also says this:
NullReferenceException: Object reference not set to an instance of an object Life$$anonymous$$anager.Update () (at Assets/Scripts/Life$$anonymous$$anager.cs:32)
Your answer
Follow this Question
Related Questions
saving a text into a player pref 0 Answers
Help making a high score with player prefs/displaying it 1 Answer
Help with making interactive text animation for a 2D game 0 Answers
Text Mesh pro won't show certain characters 1 Answer
Tooltip doesn't work 0 Answers