Hi everyone,
I keep getting this error, even though I don’t know that it’s actually causing problems:
NullReferenceException: Object reference not set to an instance of an object
ScoreLastPage.Main () (at Assets/ScoreLastPage.js:7)
Which is to do with this script, which prints the most recent score and the high score on the “game over” page:
ScoreLastPage.js
#pragma strict
public static var scoring : ScoreText;
var endText : UI.Text;
var highScoreText : UI.Text;
var highScore : int;
scoring = GameObject.Find("ScoringText").GetComponent.<ScoreText>();
function Awake() {
DontDestroyOnLoad(transform.gameObject);
}
function Start () {
Debug.Log("High Score: " + highScore);
}
function Update(){
endText.text = scoring.printer;
//BUG: high score doesn't update.
highScoreText.text = "High Score: " + highScore;
if (parseInt(scoring.go.flips) > highScore) {
PlayerPrefs.SetInt("highScore1", scoring.go.flips);
PlayerPrefs.Save();
highScore = PlayerPrefs.GetInt("highScore1");
}
else {
highScore = PlayerPrefs.GetInt("highScore1");
}
highScoreText.text = "High Score: " + highScore;
}
It draws from this script to get the current score (ScoreText.js):
#pragma strict
var go : pizza;
var text : UI.Text;
public static var printer : String;
function Awake() {
DontDestroyOnLoad(transform.gameObject);
}
function Start () {
go = GameObject.Find("pizza-small").GetComponent(pizza);
}
function Update () {
printer = go.flips.ToString();
text.text = "Score: " + printer;
}
That script takes the tracked number of flips from a script called “pizza.js”, and prints the score live, during the gameplay. Also, ScoreLastPage.Js has issues with my playerprefs and how I save them, so my questions are:
-
How can I get rid of my aforementioned error?
-
If the player gets a new score, that is overwritten as the high score, even if the value is not higher than the previous high score. For instance, if the high score is 4, and I get 1 point, the high score is set to 0.