Hey i have read through a bunch of similar questions asked but I am having trouble applying it to my existing code. I would like my high score to be displayed on the game menu but I don’t know how to extract the highest score from the score code i am using now. I know i have to set player prefs and compare high score to regular score but i am having trouble wrapping my head around it.
My score code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
public static int scoreValue = 0;
Text score;
// Start is called before the first frame update
void Start()
{
score = GetComponent<Text>();
scoreValue = 0;
}
// Update is called once per frame
void Update()
{
score.text = "Score: " + scoreValue;
}
}
Then I have that linked to when a game object (platform) gets destroyed i add 10 points. not sure if thats important but that code is as follows:
sing System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Eraser1 : MonoBehaviour
{
// Start is called before the first frame update
void OnTriggerEnter2D(Collider2D o)
{
if (o.tag == "turf1")
{
Score.scoreValue += 10;
Destroy(o.gameObject);
}
}
// Update is called once per frame
void Update()
{
}
}
Any help is greatly appreciated.
I would make your Score class into a singleton, then let your Eraser (or whatever is giving points) call into it. The singleton will deal with the score, highscore, saving, etc. Here is an example:
–
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// Just place this in your scene somewhere and let objects send it points via UpdateScore method
/// </summary>
public sealed class ScoreManager : MonoBehaviour
{
// Simple singleton pattern
static ScoreManager instance;
public static ScoreManager Instance
{
get
{
if (instance == null)
{
instance = FindObjectOfType<ScoreManager>();
}
return instance;
}
}
[SerializeField] Text highScoreText;
[SerializeField] Text currentScoreText;
int currentScore;
public int CurrentScore
{
get { return currentScore; }
private set
{
currentScore = value;
UpdateScoreText();
}
}
// When the game launches, set the high score
private void Awake()
{
SetHighScoreText();
}
// Just for testing - should delete the entire method
// Uncomment to use this as a test
//private void Update()
//{
// if (Input.GetKeyDown(KeyCode.Space))
// {
// UpdateScore(10);
// }
// else if (Input.GetKeyDown(KeyCode.Return))
// {
// CheckForNewHighScore();
// }
//}
// Call this method to add more points (which will auto update the text for you)
// Called like this ScoreManager.Instance.UpdateScore(10); from wherever points are given
public void UpdateScore(int delta)
{
CurrentScore += delta;
}
private void UpdateScoreText()
{
currentScoreText.text = CurrentScore.ToString();
}
private void SetHighScoreText()
{
// We try to find a highscore, if there isn't one, we default to 0
int highScore = PlayerPrefs.GetInt("HighScore", 0);
highScoreText.text = highScore.ToString();
}
// Game ended - call this to set new highscore if there is one (and save it)
public void CheckForNewHighScore()
{
int highScore = PlayerPrefs.GetInt("HighScore", 0);
if (currentScore > highScore)
{
PlayerPrefs.SetInt("HighScore", currentScore);
PlayerPrefs.Save();
// Update high score text
SetHighScoreText();
}
}
}
–
No need to use an update method for scores. Just let the property deal with it when its updated. This makes sure that whenever a score gets updated, so does the text.
Hope this helps a little.