I have a UI Text that gives a score based off of how far the player has traveled using this code:
public Transform player;
public Text scoreText;
scoreText.text = player.position.x.ToString("0");
I want to display the value of scoreText on the GameOver scene that appears when the player collides with an obstacle. The way I am currently doing this is by using DontDestroyOnLoad with the following code:
private void Awake()
This successfully takes the score and puts it on the next scene. However, when I press a retry button to take me back to the previous scene, I now have a duplicate of the score overlapping with the original. In other words, every time I play my game, I get a new copy of the score.
Can someone help me find a way to prevent these duplicates from appearing every time I run the game? I only want the original score that keeps going up rather than the score from the last game.
You are asking Unity to keep your object, even when you load a new scene, so your old object is still there, while you create a new one, and so on. If you want only one object, even when loading the scene again you should make a Singleton approach and make sure you are only instantiating the object once.
The way I did it in my game is that I tagged the object that I wanted to persist on load and then used
if (GameObject.FindGameObjectsWithTag("MusicPlayer").Length > 1) Destroy(GameObject.FindGameObjectWithTag("MusicPlayer"));
This will only destroy one of the MusicPlayers if there is more than one
Hello @DaProtato from the future. It is 2020.
Singleton approach will work I think, but I don’t understand it. All I find are answers saying “You should use a singleton to do this”. Thank you?
So, I accomplished what you’re asking by following this HonkBark Studios tutorial.
My final script looks like this. I added Debug.Log’s to make me feel better that it was working. In my game, there are multiple rooms (scenes) like a NES Legend of Zelda dungeon. When visiting the starting room, there would be TWO players (the original from the hierarchy of that starting room and then the one that was not destroyed via dontdestroyonload)
public class GameManager : MonoBehaviour
public static GameManager Instance;
public GameObject player;
private void InstantiateController()
if (Instance == null)
Instance = this;
else if (this != Instance)
Debug.Log("Destroying extra GM");
Debug.Log("Player has not spawned. I'll make one for you.");
Instantiate(player, new Vector2(-4, 4), Quaternion.identity);