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
10
Question by Wyern1 · Dec 28, 2011 at 07:33 PM · dontdestroyonload

A way to check if an object has DontDestroyOnLoad activated

Is there a way to check if a gameobject (or its parent) has been set to DontDestroyOnLoad?

Comment
Add comment · Show 3
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 whydoidoit · Jul 24, 2012 at 05:20 PM 0
Share

A good question that I would love to know the answer to...

avatar image Simon Says · Apr 07, 2013 at 01:28 AM 0
Share

$$anonymous$$e too. (Not enough karma to upvote.)

avatar image Unitraxx · Apr 07, 2013 at 01:31 AM 0
Share

I shall do it in your place

4 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by Simon Says · Apr 07, 2013 at 01:49 AM

Assuming that there's no official way to query the property, I've come up with a workaround:

  1. Created a simple MonoBehaviour script that calls Object.DontDestroyOnLoad(gameObject) in Awake(), and named it DontDestroyOnLoad.

    function Awake() { DontDestroyOnLoad(gameObject); }

  2. Attached the script to all objects I don't want to be destroyed, instead of doing the call from another component.

This way it's modular and I can see and modify the property in the editor. And more importantly, I can query it like this:

 var obj: GameObject;
 
 if (obj.GetComponent.<DontDestroyOnLoad>()) {
    print("The object is kept between levels.");
 }


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
5

Answer by Pacosmico · Dec 04, 2018 at 12:14 AM

unity has "gameObject.scene" for a while now
you can go all like
if (gameObject.scene.buildIndex == -1) //dontdestroy activated
nowdays

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 CaseyHofland · Jun 01, 2019 at 10:13 AM 0
Share

Thank you, worked like a charm!

avatar image sand_lantern · Oct 11, 2019 at 02:50 PM 0
Share

According to the docs, -1 means it was loaded by an asset bundle.

https://docs.unity3d.com/ScriptReference/Scene$$anonymous$$anagement.Scene-buildIndex.html

Are we positive that this works as expected?

avatar image crekri · Feb 21 at 04:16 AM 0
Share

gameObject.scene.buildIndex will return -1 for addressable, assetbundle in additional to DontDesroyOnLoad scene. Check gameObject.scene.name == "DontDestroyOnLoad" instead.

avatar image
1

Answer by mateiasu · Jun 26, 2013 at 01:55 PM

We worked around this function call with HideFlags. Worked for us. I guess HideFlags.DontSave is almost the same as DontDestroyOnLoad().

 gameObject.hideFlags = HideFlags.DontSave;
 
 if (!((gameObject.hideFlags & HideFlags.DontSave) == HideFlags.DontSave)) {
   // You may be destroyed.
 }

DontSave means that the object is not saved to the scene. Further if you load another scene this object does not get destroyed, because it is not part of the scene. Just in case you're wondering why 'dont save' even you want to 'dont destroy' it.

Ouh and you could also display a enum popup in the inspector to define what flag should be assigned. More common approach?!

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 Simon Says · Jul 03, 2013 at 08:58 PM 0
Share

From the documentation:

  // Instantiates a Plane everytime the game starts and never destroys it
 // even if you stop your game preview
 // NOTE: Watch out, this can cause LEA$$anonymous$$S
 function Start() {
     var notDestructable : GameObject = GameObject.CreatePrimitive(PrimitiveType.Plane);
     notDestructable.hideFlags = HideFlags.DontSave;
 }

It doesn't look exactly safe, does it? It appears to be meant for editor rather than for general usage.

avatar image Hosnkobf Simon Says · Jan 25, 2018 at 03:39 PM 1
Share

As long as you just check for the flag without assigning it, it shouldn't be any problem.

avatar image
0

Answer by crekri · Feb 21 at 04:15 AM

You can now use gameObject.scene.name == "DontDestroyOnLoad" to check if a scene is flagged with DontDestroyOnLoad.

Note that the above answer of checking gameObject.scene.buildIndex will return -1 for addressable, assetbundle in additional to DontDesroyOnLoad scene, and it likely isn't what you want.

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

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

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

donotdestroy for hud - but hud appears on main menu :( 1 Answer

DontDestroyOnLoad issue 1 Answer

Can you undo "dont destroy on load"? 1 Answer

dontdestroyonload object references 0 Answers

How to check scene number and use it in an If statement? 3 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