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
0
Question by MSachs · Dec 12, 2018 at 12:42 PM · destroydontdestroyonloadduplicate

DontDestroyOnLoad duplication

Hi,

I know there are countless threads about this problem but I couldn't find one that addresses my specific problem.


So basically I have my UI elements which I want to carry through all my scenes until I go back to the very first scene (where the usual duplication happens). Then I have the situation, that I would like to reset all my UI elements to the default state. That actually happens if I don't do anything due to the duplication which is then located above the original UI elements in the hierarchy and is displayed instead of the original UI.

But since I can imagine that it is not quite the best idea (regarding performance) to keep all the duplicates in the scene I need a way to delete the original version and only keep the duplicate when loading the original scene.


I tried the following code which works nicely to destroy the duplicate but keeps the original.


 using UnityEngine;
 
 public class DontDestroy : MonoBehaviour 
 {
     private void Awake()
     {
         int destroyableObjects = FindObjectsOfType<DontDestroy>().Length;
 
         if (destroyableObjects != 1)
         {
             Destroy(this.gameObject);
         }
 
         else
         {
             DontDestroyOnLoad(gameObject);
         }
     }
 }



is there a way to keep the duplicate and destroy the original?

Thanks in advance :)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Hellium · Dec 12, 2018 at 12:52 PM

 using UnityEngine;
  
  public class DontDestroy : MonoBehaviour 
  {
      private static DontDestroy original;
      private void Awake()
      {     
          if (original != this )
          {
              if( original != null )
                  Destroy(original.gameObject);
              DontDestroyOnLoad(gameObject);
              original = this;
          }
      }
  }


Comment
Add comment · Show 3 · 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 MSachs · Dec 12, 2018 at 01:14 PM 0
Share

Thanks. I see the idea behind the script and in my opinion it actually should work like that but sadly it doesn't seem to do anything.

Duplicates still get created and none of the objects get destroyed...

avatar image Hellium MSachs · Dec 12, 2018 at 01:19 PM 0
Share

$$anonymous$$y bad, original must be static

avatar image MSachs Hellium · Dec 12, 2018 at 01:27 PM 0
Share

Thanks a lot! Works like a charm!

you might want to edit in the curly brackets for the last if statement to prevent confusion for other users :)

Thanks again!

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

102 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

Related Questions

Destroy Duplicates 1 Answer

Object.DontDestroyOnLoad ? 2 Answers

Destroy Object when scene loads 1 Answer

Save state of sprite between scene switching 0 Answers

How do I ensure no more than one instance of each of many GameObjects? (Singleton doesn't seem to scale well this way.) 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