• 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
6
Question by alex.shen · Dec 27, 2011 at 09:53 AM · gameobject

How to mark an object destroy on load again when DonDestroyOnLoad was called ?

If I call DontDestroyOnLoad on an object, then this object wont't be destroyed on load. Is there any equivalent like DestroyOnLoad to destroy the object automatically when loading ?

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
Best Answer

Answer by BiG · Dec 27, 2011 at 10:46 AM

No, standing on my knowledge, you have to script that. It's pretty easy, and it's something like this, depending on your needs:

function Start(){
   if (conditions on destroy hold...)
      Destroy(a particular gameObject)
}

The motivation in a missing function of that type relies on the fact that you can destroy an object whenever you want (so, you can do it with a script like the previous one), but the inverse isn't true: when the new scene is loaded, objects are implicitly destroied, so the DontDestroyOnLoad() is required as an "exception" at that rule.

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 alex.shen · Dec 27, 2011 at 12:07 PM 0
Share

Yes, this can serve my need. But what I expected is an easier way like just calling DestoryOnLoad. Anyway, I'll implement like you suggested.

avatar image Albertkaruna · Nov 14, 2015 at 11:00 AM 0
Share

Thank you BiG. Its working fine...

avatar image SpicyCatGames · Nov 06, 2020 at 04:44 PM 0
Share

This is completely incorrect.

Start is called exactly once in the lifetime of the script

https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.Start.html
So after you call DonDestroyOnLoad, this condition will never be checked.

@Hetmanus 's answer is the one that actually works

avatar image
0

Answer by losingisfun · Jan 07, 2017 at 11:48 AM

I know this is an old question, but I thought someone might like to know this.

Destroy(gameObject) will destroy the object immediately, but you may still want the object until the end of the scene. You can simply SetParent() to another GameObject that WILL be destroyed on load.

It's a simple concept i've used myself. Regardless of the DoNotDestroy exception, objects will still inherit that exception from their parents. You can use this to your advantage.

SetParent(); takes 1 argument, which is the Transform of the object you want to parent to.

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 losingisfun · Jan 07, 2017 at 11:52 AM 0
Share

a proper use of this would look like: gameObject.transform.SetParent(other.transform);

avatar image
10

Answer by Hetmanus · Feb 10, 2017 at 09:17 AM

SceneManager.MoveGameObjectToScene(gamObject, 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 Pilz · May 22, 2019 at 01:50 AM 0
Share

Hetmanus solution worked for me. Pretty simple and easy. That is equivalent DestroyOnLoad

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

line render from object to object 1 Answer

How to make gameobject follow mouse pointer that's on rotated orthographic camera? 0 Answers

Ray Casting - Trigger function 1 Answer

Any way to lock an object from changes in the editor? 4 Answers


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