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 /
  • Help Room /
avatar image
4
Question by arkon · Jan 10, 2016 at 02:37 AM · bugwarnings

how to make the warning DontDestroyOnLoad only work for root GameObjects go away?

I keep getting this warning every time I try and use DontDestoyOnLoad:-

"DontDestroyOnLoad only work for root GameObjects or components on root GameObjects."

This didn't show up pre Unity 5.3 so whats up?

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 LazyElephant · Jan 10, 2016 at 08:41 AM 0
Share

Is the object you're using it on the child of another object by any chance?

avatar image arkon · Jan 10, 2016 at 09:39 AM 0
Share

One of them was yes, I've had to split the game object into two parts now, one in the root with the script and the rest in the NGUI child object. The other 2 warnings are from the Prime31 plugins which I have no control over. This new behaviour is a right pain in the ass.

Thanks for the help though.

avatar image RandomCharacters · Mar 23, 2016 at 03:41 PM 0
Share

I get the same problem. it just starting happening with latest unity.

DontDestroyOnLoad only work for root GameObjects or components on root GameObjects.

2 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by masterton · Sep 26, 2017 at 06:10 PM

Unity calls this a warning but its more of an error because your child object WILL be destroyed on a scene switch. Add and call the function below instead. It goes up the parent chain and sets the DontDestroyOnLoad flag on the root object.

     public static void DontDestroyChildOnLoad( GameObject child )
     {
         Transform parentTransform = child.transform;
 
         // If this object doesn't have a parent then its the root transform.
         while ( parentTransform.parent != null )
         {
             // Keep going up the chain.
             parentTransform = parentTransform.parent;
         }
         GameObject.DontDestroyOnLoad(parentTransform.gameObject);
     }
Comment
Add comment · Show 5 · 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 figurefurther · Jul 07, 2018 at 06:05 PM 0
Share

Thanks masterton! this worked like a charm.

avatar image Skelly1983 · Jul 18, 2018 at 04:50 PM 0
Share

Love this approach, solved my issue I had.

avatar image EpicJoey · Dec 28, 2018 at 06:41 AM 0
Share

does not work for me

avatar image EpicJoey · Dec 28, 2018 at 06:44 AM 0
Share

When I add it to a button in a canvas it makes everything in the canvas not be destroyed

avatar image TheDemiurge · Mar 11, 2019 at 10:36 PM 1
Share

You guys know about transform.root, right? Just call

DontDestroyOnLoad(transform.root.gameObject);

avatar image
2

Answer by Salim · Jun 05, 2016 at 12:38 PM

I'm also having this error message when my NetworkManager object is a child in a parent object.

Simply putting it back to the root of the Hierarchy fixes it, but this is a problem if you want to organize your hierarchy with empty objects that act as folders.

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

46 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

Related Questions

Index was out of range. error 1 Answer

Bug - circular shadow from unknwon source under camera, with size of shadow distance from quality settings 1 Answer

Unity's Editor UI Frozen/ Not updating, Video included 0 Answers

Weird texture behaving on flat object 0 Answers

Is it possible to be on both the App thread and the UI thread? -1 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