- Home /
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" );
}
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!
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.
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;
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
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.
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.
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
![](https://koobas.hobune.stream/wayback/20220613103326im_/https://answers.unity.com/themes/thub/images/avi.jpg)