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;
void Update()
{
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:
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.
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?
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)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public static GameManager Instance;
public GameObject player;
void Awake()
{
this.InstantiateController();
}
private void InstantiateController()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(this);
}
else if (this != Instance)
{
Debug.Log("Destroying extra GM");
Destroy(this.gameObject);
}
}
void Start()
{
Debug.Log("Started");
Debug.Log("Player has not spawned. I'll make one for you.");
Instantiate(player, new Vector2(-4, 4), Quaternion.identity);
}
}