Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by DaProtato · Jan 02, 2018 at 09:15 PM · uiscene-switchingdontdestroyonloadhowduplicate

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OneCept-Games · Jan 02, 2018 at 09:21 PM 0
Share

link text

avatar image
0

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);
     }
 }


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Recluse · Sep 11, 2020 at 03:38 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

125 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges