I’m making a high score on my game (Brackeys How To Make A Video Game), and have added random gen infinite levels, and to get into one of the infinite modes you go through a menu that has a button for easy, medium, and hard. Here I want it to show your high score. The score is how far you have went (amount of blocks) in the infinite mode, but I have to reference the score in the infinite mode to the menu for the high score.
I have looked at other posts that tell you to link the data to a GameObject, but it is a separate scene, so I would have to put a score text GameObject in, but would have no reference to the rb.position.z (players y coordinate)
I hope I explained this well
What you’re looking for is basically called a GameManager.
ULTRA-simple static solution to a GameManager:
OR for a more-complex “lives as a MonoBehaviour” solution…
Simple Singleton (UnitySingleton):
Some super-simple Singleton examples to take and modify:
Simple Unity3D Singleton (no predefined data):
Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:
These are pure-code solutions, do not put anything into any scene, just access it via .Instance!
If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:
public void DestroyThyself()
{
Destroy(gameObject);
Instance = null; // because destroy doesn't happen until end of frame
}
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameManager : MonoBehaviour {
bool gameHasEnded = false;
public float restartDelay = 1f;
public GameObject CompleteLevelUI;
public Text TextUI;
public Text highScore;
public void SetHighScore() {
highScore.text = PlayerPrefs.GetInt("highScore", TextUI).ToString();
}
public void CompleteLevel ()
{
CompleteLevelUI.SetActive(true);
}
public void EndGame ()
{
if (gameHasEnded == false)
{
PlayerPrefs.SetInt("highScore", TextUI);
gameHasEnded = true;
Debug.Log("GAME OVER");
Invoke("Restart", restartDelay);
}
}
void Restart ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
Hey, its giving me this error when I use this
Assets/scripts/GameManager.cs(29,39): error CS1503: Argument 2: cannot convert from ‘UnityEngine.GameObject’ to ‘int’
and when I define it as a int it can’t have the “TextUI” value
Specifically if you want to take the integer out of Playerprefs and manipulate it, it has to stay an integer, not get turned into a string. You are also trying to pass the UIText Object. That’s not a string or an integer.
If all you need is high score storage, here’s something much simpler:
Tracking simple high score / low time single-entry leaderboard:
Check out some basic C# tutorials on data types so you can understand the difference between a number 123 and the string “123” so you can reason about the issue at hand.
// these are NOT the same thing
int number1 = 123;
string number2 = "123";
Both perfectly legal, both has a place and a purpose. Go learn why.