using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour {
public static int score;
public static int highscore;
public Text guiText;
void Start()
{
score = 0;
highscore = PlayerPrefs.GetInt ("highscore", highscore);
}
void Update()
{
if (score > highscore);
highscore = score;
PlayerPrefs.SetInt ("highscore", highscore);
guiText.text = "HS: " + highscore;
}
public static void AddPoints (int pointsToAdd)
{
score += pointsToAdd;
}
public static void Reset()
{
score = 0;
}
}
system
2
You are using the built-in UI system of unity. You’re not using the GUIs. If you want to continue using the UI system:
- You need to include “using UnityEngine.UI;”
- You need to add Text in your hierarchy and assign it once your script is attached to a empty GameObject
or… You could use GUIs. The code goes like this:
public static int score;
public static int highscore;
void Start()
{
score = 0;
highscore = PlayerPrefs.GetInt ("highscore", highscore);
}
void Update()
{
if (score > highscore);
highscore = score;
PlayerPrefs.SetInt ("highscore", highscore);
}
void OnGUI()
{
GUI.Label(new Rect(100, 60, 300, 40), "High Score:");
GUI.Label(new Rect(200, 60, 300, 40), highscore.ToString());
}
public static void AddPoints (int pointsToAdd)
{
score += pointsToAdd;
}
public static void Reset()
{
score = 0;
}