Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
4
Question by badneighbor · May 26, 2010 at 10:22 PM · loadleveldontdestroyonload

undoing DontDestroyOnLoad without immediately destroying the object

After calling DontDestroyOnLoad on a game object that we want to pass between levels. Eventually we want to finally destroy it... say to reset the game to the initial state.

Is there a way of undoing DontDestroyOnLoad without immediately destroying the object..

the command: Destroy(this.gameObject); will cause issues if other game objects that are in the same scene still depend on it. I just want it to get destroyed with all of the other game objects on the next level load.

Comment
Add comment · Show 1
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 Daniel 6 · May 26, 2010 at 10:26 PM 0
Share

I to wish you could write something like DontDestroyOnLoad (go, true); The boolean would say wether it is destroyed or not on load.

4 Replies

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

Answer by Mike 3 · May 26, 2010 at 11:26 PM

No way to do it automagically no

best way i can think of doing it is this:

var destroyOnLoad = false;

function OnLevelWasLoaded(level : int) { if (destroyOnLoad) Destroy(gameObject); }

that way you get your object destroyed before the first update in the new level

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 badneighbor · May 27, 2010 at 05:51 PM 1
Share

Due to event execution ordering the above code was giving me runtime threading errors.

I think the issue is that Destroy does not take action immediately and there is some overlap between its call, other event phases of the event loop, and its action.

The run-time error goes away if I do replace Destroy with DestroyImmediate. (however there could be still un-reported errors)... thats it for now.

avatar image
0
Best Answer

Answer by Eagle32 · Jun 27, 2010 at 12:06 PM

If you're destroying it and it's then being recreated for the level you've just loaded to (I'm assuming this from your comment about destroy and destroyimmediate) why don't you make a reset method for the object that puts it back in its initial state.

That way you could just replace the destruction with a reset and carry on.

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
10

Answer by MikeHergaarden · Jun 14, 2017 at 08:18 PM

@badneighbour To undo a DontDestroyOnLoad, simply make a new GameObject and set it as parent of the object that was previously DontDestroyOnLoad.

 GameObject bla= ...something;
 DontDestroyOnLoad(bla);
 
 GameObject newGO = new GameObject();
 bla.transform.parent = newGO.transform; // NO longer DontDestroyOnLoad();
Comment
Add comment · Show 2 · 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 Powerpaul · Jun 25, 2017 at 03:16 PM 0
Share

Works like a charm, thanks!

avatar image DarnVisages · Jun 12, 2018 at 03:14 PM 1
Share

For convenience, I used this concept in an extension method class so I can remove any object from DontDestroyOnLoad by calling a method on its gameObject:

 using UnityEngine;
 
 public static class Extension$$anonymous$$ethods {
     public static void RemoveFromDontDestroyOnLoad(this GameObject go)
     {
         GameObject newGO = new GameObject();
         go.transform.parent = newGO.transform; // NO longer DontDestroyOnLoad();
     }
 }

so with this, you can call it on any GameObject like this:

 gameObject.RemoveFromDontDestroyOnLoad();
avatar image
8

Answer by Alverik · Aug 28, 2018 at 02:09 AM

Like I said in another post: DontDestroyOnLoad() simply moves an object to the special Scene "DontDestroyOnLoad", so all you need to do is move it back to the active scene:

 SceneManager.MoveGameObjectToScene(TargetGo, SceneManager.GetActiveScene());
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 GroznyBear · Nov 23, 2018 at 09:42 AM 0
Share

One extra note, TargetGo should be the root of the hierarchy by itself, ie un-parented from any objects, like: TargetGo.transform.SetParent(null);

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

6 People are following this question.

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

Related Questions

Method execution order after LoadLevel()? 1 Answer

Spawn carachters on locations and destroy the manager after level load 1 Answer

How to begin animated skybox at same point in sequence as when the scene ended? 0 Answers

DontDestroyOnLoad but script being excuted multiple times 1 Answer

how can i make my character to be on another scene? 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