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
2
Question by Talantyyr · Mar 12, 2014 at 04:27 PM · dontdestroyonloadapplication.loadlevel

DontDestroyOnLoad calls Awake() and Start() function each time the scene is reloaded

The function DontDestroyOnLoad doesn't seem to work for me.

     void Awake () {
         DontDestroyOnLoad(gameObject);
         Debug.Log ("awake called");
     }
 
     void Start () {
         Debug.Log ("start called");
     }
 

Both debug messages are displayed each time i call Application.LoadLevel(0);

Is this the intended behaviour or am i doing something wrong? The Script is attached to a GameObject.

Comment
Add comment · Show 1
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 onyarao · Mar 09, 2015 at 11:12 AM 0
Share

I have the same issue. I'm using Unity 5 and the GameObject is a root object.

 function Awake() {
   DontDestroyOnLoad(gameObject);
   Debug.Log("awake called");
 }

It's called every time I call :

 Application.LoadLevel(0);

4 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by fafase · Mar 09, 2015 at 11:26 AM

I would think it is the intended behaviour just not the one you intended.

You mentioned that it happens every time you load the level 0. I would assume you have in your scene an object holding that script. So each time you load the scene, this object is created while the previous one is also kept. So the first time you see the Start and Awake of the first object and any time you come back, you get the Start and Awake of the new object that is created with the new loading.

Looking at your hierarchy, you would probably see multiple times the same object.

Solution:

 public class MyClass:MonoBehaviour{
     private static MyClass instance = null;
     void Awake(){
         if(instance == null){
              instance = this;
              DontDestroyOnLoad(gameObject);
         }else if(instance != this){
              Destroy(this.gameObject);
              return;
         }
     }
 } 



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 Bonfire-Boy · Mar 09, 2015 at 01:34 PM 2
Share

No, a static variable is only initialised once. That's the point of it. fafase's solution does has a typo though (`instance == this` should be single '=')

avatar image fafase · Mar 10, 2015 at 06:16 PM 0
Share

I did remove the typo :)

avatar image onyarao · Mar 10, 2015 at 06:19 PM 0
Share

Thank you very much, I was expecting too much that it behaved like I was expected to !

avatar image
6

Answer by HankChinaski · Sep 04, 2016 at 11:18 PM

For anyone who might come across this question, a clone of your GameObject is instantiated every time you load this scene, so chances are, those calls are being made by the clone.

Don'tDestroyOnLoad() does not call Awake() or Start() when a new scene is loaded.

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
2

Answer by tormentoarmagedoom · Jul 09, 2016 at 02:39 PM

Just one thing.

Scripts only calls Start and Awake when they are loaded for first time. If you use DontDestroyOnLoad() in a script, or object with scripts, all that scripts will NOT CALL start / 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
avatar image
0

Answer by WILEz1975 · Sep 12, 2016 at 02:48 AM

...and if i want to call a function on the start/awake (in a new loaded scene) on a Don'tDestroyOnLoad() object?

Comment
Add comment · Show 4 · 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 HankChinaski · Sep 12, 2016 at 11:01 PM 6
Share

OnLevelWasLoaded() should do the trick. Example:

using UnityEngine; using System.Collections;

public class A : $$anonymous$$onoBehaviour {

 void Start(){
     DontDestroyOnLoad (gameObject);
 }

 void OnLevelWasLoaded(){
     Debug.Log ("A new scene was loaded");
 }

}

avatar image $$anonymous$$ HankChinaski · Nov 08, 2017 at 07:36 PM 0
Share

Been searching for an answer to this. Thank you @HankChinaski. Won't be forgetting OnLevelWasLoaded() very soon. Cheers mate.

avatar image MrJamieMcC HankChinaski · Apr 28, 2018 at 03:58 PM 0
Share

Thanks for this

avatar image PMacDev MrJamieMcC · Sep 08, 2020 at 06:57 PM 0
Share

OnLevelWasLoaded was deprecated, you should now be using this: https://docs.unity3d.com/ScriptReference/Scene$$anonymous$$anagement.Scene$$anonymous$$anager-sceneLoaded.html

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

26 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

Related Questions

DontDestroyOnLoad 0 Answers

Don't lose variable data when reloading a level - c# 3 Answers

'DontDestroyOnLoad' is destroying other gameobjects 1 Answer

Application.LoadLevel object clone spam 0 Answers

DontDestroyOnLoad Missing Object 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