Hello everyone, I’m having some real trouble trying to understand the interation between LoadLevel() and OnLevelWasLoaded(), because right now it seems that my script continues to execute before the load is finished, leaving a -very- awkward “screen flash” effect where Unity has not finished un-loading old objects, but has already partially loaded the next scene.
In Detail:
What I’m doing when the player wants to go from in-game to the main menu is put up some UI that tells them what is going on, loads the new level, then explicitly destroys a set of objects that have been tagged with DontDestroyOnLoad() [which includes UI elements]. However, what is happening is that after everything is supposedly loaded, my objects are destroyed but the game clearly still has many of the old objects from the last scene still visible, and has not finished loading the new scene. This makes a very startling “screen flash” where the user can see a partially un-loaded game with no UI to hide it. So it seems like my script is running before the level is actually loaded (and in fact even OnLevelWasLoaded() runs despite no load having been completed.
The script I posted shows a section from a longer script (just the relevant part) as well as the OnLevelWasLoaded() method. The image shows what happens during the transition, LoadLevel() is called, but the old objects are still there, yet OnLevelWasLoaded() has been called and as a result the UI (and other objects) have just disappeared, leaving an awkward “screen flash” effect.
Any ideas on what is going on is appreciated, thank you for reading.
else if(info == UI_LoadingScreenTransitions.RealToMain)
{
//set text based on context (we can do this because the Real Loading Screen still exists):
realLoadingScreenText.text = "Returning to Port...";
//image can be left alone since it should already be correct...
//throw to real loading image...
//Debug.Log("showing real transitional image...");
realLoadingScreenHierarchy.alpha = 1f;
yield return new WaitForSeconds(minTime); //a min waiting time to prevent extremely fast transition...
//Debug.Log("Tracker_LoadLevels() breaking");
//Debug.Break();
//12 15 2014 at this point, nothing special is done, just go back to the main screen...
Application.LoadLevel(mainMenuSceneName);
//when Main Menu comes up, loading screen will be cleared, no additional action needed...
Debug.Log("Tracker_LoadLevels() breaking");
Debug.Break();
OnLevelWasLoaded(0);
}
}
void OnLevelWasLoaded(int level)
{
if(level == 0)
{
//this GO/hierarchy must be destoyed manually because it is protected:
//...put this last because deleting the UI causes issues with the Player ship and missing references...
persistantUIElements = GameObject.Find("0PersistentThroughLoad").GetComponent<Transform>();
Destroy(persistantUIElements.gameObject);
Debug.Log("0PersistentThroughLoad object destroyed()");
}
}