OnLevelWasLoaded not called at all
Hello. I have a problem. My project has only one scene. I need get event like OnLevelWasLoaded to apply some additional settings to my game objectst. Unfortunally this method not called at all. I put some breakpoints in debugger to watch execution of this method, but they weren't called after game starting. P.S. I start game from editor. May be it will work if i build and run standalone app?? P.P.S Awake methods in the same objects are called, but OnLevelWasLoaded aren't.
Here some of my code:
public class ConfiguratorFile : MonoBehaviour {
public void OnLevelWasLoaded(int level) {
Debug.Log("Level loaded"); //just for test
//this method is never called :(
}
public void Awake() {
//this method is called
//DocumentManager.Instance.LoadFromFile("links.xml"); //singleton
}
}
This scipt is attached to empty gameobject.
Answer by zanyang1103 · Jan 05, 2016 at 10:06 AM
alright ,i got the same problem...
i was working on a demo project which only contains one scene
in the editor mode ,OnLevelWasLoaded was not called when i first press play button,then it was called everytime after i used SceneManager.LoadScene(SceneManager.GetActiveScene().name); to reload the scene。
however,in the .exe file i build from this project ,it was called right after the game started.
so here is what i thought, in editor when we start the scene ,the environment is diffent from the real game environment,and the editor doesn't consider this as a new scene loaded。
to prove this ,i add a new scene which only contains a camera ,and add a script with this:
void Start()
{
SceneManager.LoadScene("main");
}
"main" was the name of my previous scene,so when the new scene loaded ,it just load my main scene. this time ,OnLevelWasLoaded was called as it should be.
don't know why they design it this way,hope this will help you.
Had the exact same problem and I'm still curious as to why it works differently in the editor and the .exe is it a bug or are we just doing something wrong ?
Answer by Susajin · Dec 14, 2015 at 10:27 AM
as far as i know OnLevelWasLoaded will not be called when you just start the game.
Answer by avetokhin · Jan 12, 2016 at 03:08 AM
I have the same problem. Create new project -> Add empty object with such script:
void Awake ()
{
print ("Awake");
}
void OnLevelWasLoaded (int index)
{
print ("OnLevelWasLoaded with index " + index);
}
When I run it in editor OnLevelWasLoaded is not invoked on start, but when I build exe file it is. It looks strange and must be a bug. Actually I faced with this problem in roguelike-2d tutorial when tried to test build.
yup, i had same issue with roguelike tutorial. only it didn't run in exe either. weird
Your answer
Follow this Question
Related Questions
Since OnLevelWasLoaded is deprecated (in 5.4.0b15) What should be use instead? 3 Answers
OnLevelWasLoaded Problem was Found On 2 Answers
OnLevelWasLoaded was not called 0 Answers
OnSceneWasLoaded deprecated - cannot find work around 2 Answers
OnLevelWasLoaded deprecated, trying to setup my buttons 1 Answer