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
3
Question by agentsmith · Jun 01, 2013 at 07:49 AM · dontdestroyonloadonlevelwasloaded

Is OnLevelWasLoaded supposed to be called twice when using DontDestroyOnLoad?

I notice if I use DontDestroyOnLoad then OnLevelWasLoaded is called twice. I've tested this using Windows 7 x64 Unity v4.1.3f3.

In gameobject's that aren't using DontDestroyOnLoad then OnLevelWasLoaded gets called only once.

Anyone else experiencing this?

This is an interesting output from a game object that uses DontDestroyOnLoad:

     private bool runOnce = true;
     void OnLevelWasLoaded ( int level )
     {
         print ( "here" );
         if ( runOnce )
         {
             runOnce = false;
             print ( "runOnce " + runOnce );
 
         }
         else
         {
             print ( "else statement" );       
             runOnce = true;
         }
     }
 
 Output:
 here
 runOnce false
 here
 runOnce false


If I do this it only prints out once...

 private bool runOnce = false;
     void OnLevelWasLoaded ( int level )
     {
         if ( runOnce )
             return;
 
         runOnce = true;
         StartCoroutine ( Tester ( ) );
         
     }
     
     IEnumerator Tester ( )
     {
         yield return new WaitForSeconds ( 1 );
         print ( "TESTING" );
     }
Comment
Add comment · Show 6
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 basti · Jun 18, 2013 at 01:22 PM 0
Share

same problem here, with an old 3.5 version. Any ideas?

avatar image sirfaty · Jun 29, 2013 at 02:11 AM 0
Share

Are you sure there isn't a second object that also has your script component in the new scene being loaded? If so, OnLevelWasLoaded will be called on the object from the original scene that is marked as DontDestroyOnLoad, as well as the new object freshly loaded from the new scene. To test for this, try making your "runOnce" variable static, and/or print out GetInstanceID() to be sure you know which object things are co$$anonymous$$g from. Good luck!

avatar image DevonIsland · Jun 29, 2013 at 02:42 AM 0
Share

This was happening to me too. Like sirfaty said, it turned out to be duplicates of the same object.

I got around it by putting a "spawner" kind of object in the scene ins$$anonymous$$d of the object itself. The spawner checks for an object of a certain type, and creates one if none are found.

It works alright with singletons, especially if you don't know what scene you'll be starting in/revisiting.

avatar image KarnEdge · Nov 25, 2013 at 11:03 PM 0
Share

This seems to be still happening in Unity v4.3.0f4

avatar image AK123 · Apr 15, 2015 at 01:16 PM 0
Share

still happening in version 5

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by neoRiley · Aug 11, 2015 at 08:31 PM

Just a simple check like the line below caught the duplicate (if you're using a singleton pattern):

 if( this != instance ) return;
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
1

Answer by Driseus · Jun 17, 2015 at 03:18 PM

You will have 2 gameobjects cause u dont destroy the old one and a new instance of the old gameobject will be created too

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 hulahoolgames · Jun 25, 2015 at 09:58 AM

         using UnityEngine;
             using System.Collections;
             
             public class Foo : MonoBehaviour {
             
                 private static Foo m_Instance;
                 
                 public static Foo instance {
                     get {
                         if(m_Instance == null) {
                             m_Instance = GameObject.FindObjectOfType<Foo>();
                             DontDestroyOnLoad(m_Instance.gameObject);
                         }
                         
                         return m_Instance;
                     }
                 }
                 
                 public override void Awake() {
                     if(m_Instance == null) {
                         m_Instance = this;
                         DontDestroyOnLoad(gameObject);
                     }else {
                         Destroy(gameObject);
                     }
                 }
             
                 void OnLevelWasLoaded(int level) {
                     if(m_Instance == this) {
                         Debug.Log(" Static Instance OnLevelWasLoaded is called");
                     } 
                 }
             }


I have this script attached to a game object in my scene. When I reload the scene in code, I see that OnLevelWasLoaded is called 2 times, but the Debug.Log statement is only hit once. So from my understanding, when a scene is loaded new instance of the script is created. The OnLevelWasLoaded is called once for each of the two instances of the script.

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 Driseus · Jun 25, 2015 at 11:02 AM 0
Share

Yes when you load the scene it creates all gameobjects that are in the scene and dosent destroy your Foo object cause u use DontDestroyOnLoad(). So you have two Foo Objects in your scene and the OnLevelWasLoaded will be called on both instances.

avatar image hulahoolgames · Jun 25, 2015 at 11:07 AM 0
Share

Yea that is what even my thinking was. I wanted to point out this through an example so people understand whats going on here and dont think thats it something fishy that unity is doing. Thanks for your reply!

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

24 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

Related Questions

change player location when coming back to a level 2 Answers

OnLevelWasLoaded still called from destroyed gameobject 2 Answers

Bizarre Co-routine errors after re-loading scenes 2 Answers

DontDestroyOnLoad - is it intended behavior? 3 Answers

...Why does this FAIL? - DontDestroyOnLoad()?? 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