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" );
}