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 /
avatar image
1
Question by SpartanSperos · Sep 02, 2015 at 09:39 AM · dontdestroyonload

DontDestroyOnLoad Not Working At All

I have quite a few objects which I'd like to keep for the next three scenes (about 25 objects total). I made the script below, and attached it to all the objects. Unfortunately, the objects destroy when I test the game and make it to the next level. There are no script errors, and I can't see anything wrong with the scene I've set up. Basically, the script does nothing and I'm not sure why. If anyone knows common problems with DontDestroyOnLoad, or anything else that might help, it would be much appreciated.

 #pragma strict
 
 function Start () {
         DontDestroyOnLoad (transform.gameObject);
     }
Comment
Add comment · Show 5
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 iamsidv · Sep 02, 2015 at 10:23 AM 0
Share

is the start function being called?

avatar image frarees · Sep 02, 2015 at 11:26 AM 0
Share

When do you actually change the scene? As @iamsidv states, check if Start is at all called. If it's not, that may be caused by Application.LoadLevel being called before e.g. Awake

avatar image SpartanSperos · Sep 02, 2015 at 01:54 PM -1
Share

The scene changes aprox. 2 $$anonymous$$uets in. How would I tell if the start function is being called?

avatar image Hellium · Sep 02, 2015 at 01:55 PM 0
Share

With a simple Debug.Log maybe ? >.<

avatar image SpartanSperos · Sep 02, 2015 at 02:46 PM 0
Share

To Unity Docs....

3 Replies

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

Answer by andsee · Sep 02, 2015 at 12:48 PM

One other thing to check is that these objects are in the root of the hierarchy. If not their parent object may get destroyed which unfortunately means their demise too.

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 SpartanSperos · Sep 02, 2015 at 02:05 PM 0
Share

Well I knew I wasn't very bright, but this was a really stupid mistake. Yeah, I had them grouped under something that got destroyed. Thanks.

avatar image GameDev_Chuck · Aug 18, 2017 at 07:37 PM 0
Share

Wooooooooooooowwwww..... Tried to do a little hierarchical cleanup the other day and stuck my game manager objects (that's supposed to persist between scenes) under an empty with w few other items.... Good God, was $$anonymous$$ring my hear out. Debug logs everywhere. Thank you sir. You are a gentleman and a scholar.

avatar image
0

Answer by pmaloo · Sep 02, 2015 at 12:00 PM

Put it in Awake.

If you put it in start, that object can be destroyed before start is called by actions in Awake/OnEnable

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 SpartanSperos · Sep 02, 2015 at 01:55 PM 0
Share

I originally did have it as Awake, but that wasn't working so I changed it to Start.

avatar image pmaloo · Sep 02, 2015 at 01:59 PM 1
Share

@SpartanSperos Check the answer by @andsee. Its a very common mistake to make the DoNotDestroyOnLoad a child of perishable parent.

Also make sure that you are not specifically destroying the object from another script.

avatar image SpartanSperos · Sep 02, 2015 at 02:46 PM 0
Share

Yeah, that was the problem. Thanks.

avatar image
0

Answer by unity_VYAo9PCTYYamFA · Jun 11, 2021 at 07:58 PM

Put it in First line of Awake function it will solve. because it need to execute first,Put it in awake

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

10 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

Related Questions

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

how to Undo DontDestroyOnLoad 1 Answer

Will DontdestroyonLoad() Keep Child Objects too 2 Answers

Keeping score problems dontdestroyonload 2 Answers

GUI Elements Carry Over To Next 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