• 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 Ben Humphreys · May 09, 2013 at 08:41 AM · parentchilddontdestroyonload

DontDestroyOnLoad Child Object

I've tested this out and it seems to be the case, but I wanted confirmation.

I have two objects, Parent and Child. Child is a child of Parent in the scene hierarchy. Child has a script on it, that calls the following in its Start():

     DontDestroyOnLoad(gameObject);

And then changes scene.

In the new scene, Child is not there.

It seems to be if the object with DontDestroyOnLoad is a child of something, the DontDestroyOnLoad() call is not respected.

Is this correct, expected behaviour? I was expecting it to appear in the new scene without any parent. It's going to be a real pain to move it out from under its parent before the scene changes.

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

2 Replies

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

Answer by Bunny83 · May 09, 2013 at 08:55 AM

This is the correct behaviour. Only direct scene objects are saved, so you should unparent your child object before you load the new scene. You can simply do this by using BroadcastMessage on the parent object. Just have your script on the child react to the message and unparent itself.

It would be great if there was a OnLevelLoading which would be called before the level loads in contrast to OnLevelWasLoaded, but there is no such function. What "might" also work is to unparent the object in OnDisable, but it could already be too late.

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 Ben Humphreys · May 09, 2013 at 09:04 AM 0
Share

Thanks for the reply!

How do you unparent an object? child.transform.parent = null doesn't seem to work.

avatar image Bunny83 · May 09, 2013 at 09:26 AM 0
Share

Sure that does work ;)

This should work:

 // in the script of the child object
 // C#
 void OnLoadingLevel()
 {
     transform.parent = null;
 }

 // Where you call LoadLevel
 parentObject.Broadcast$$anonymous$$essage("OnLoadingLevel");
 Application.LoadLevel( ... );
avatar image alvaro-em · Dec 14, 2015 at 12:00 PM 1
Share

It worked like a charm, but just a quick piece of advice: I realized that the DontDestroyOnLoad option had been removed once the children were unparented, so I had to add it again in the OnLoadingLevel method.

avatar image
2

Answer by liszto · May 09, 2013 at 08:57 AM

Yes. When the engine destroy all the object in the current scene to move to the next he checks each object (in the first hierarchy level) so if your parent doesn't have

   DontDestroyOnLoad(gameObject);

it will be destroy and he will not check its children even if these ones have "DontDestroyOnLoad"

==> You can read a previous Bunny's answer here ;)

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.

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

16 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

Related Questions

Make a simple tree 1 Answer

Becoming parent after OnTriggerStay/Enter? 1 Answer

How can I activate an empty object which is parent of child objects? 1 Answer

Is there a way to make a selected object a child of another object during the game? 1 Answer

Child affecting parent rigidbody? 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