- Home /
How to get rid of the duplicates created by DontDestroyOnLoad?
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:
private void Awake()
{
DontDestroyOnLoad(gameObject);
}
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.
Answer by leSamo · Jan 02, 2018 at 09:20 PM
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
Answer by OneCept-Games · Jan 02, 2018 at 09:18 PM
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.
Answer by babasuter · Jun 20, 2020 at 03:21 PM
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)
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);
}
}
The singleton approach is fine for objects which require only one instance, like a player or a game manager.
But what if you need multiple instances of an object to persist between scenes?
For example, several elevators which take the player to other scenes and which persist after loading to hide the transition (doors open to reveal newly loaded scene)?
In this case, returning back to the previous scene will result in a duplicate elevator which cannot be removed by checking for another instance - because multiple instances are needed.
In this case, we would need to assign the elevator a unique ID to check for a duplicate of.
Your answer
Follow this Question
Related Questions
How do you transfer UI Text from one scene to another? 1 Answer
Best way to deal with DontDestroyOnLoad when returning back to the same scene? 1 Answer
Multiple EvenetSystems in Scene - only have 1 after searching though 1 Answer
How to persist player inventory using dictionary between scenes 2 Answers
How to keep the same gameobject with different transform values in different scenes 1 Answer