How to use OnLevelWasLoaded ?

I’m trying to use OnLevelWasLoaded but I cannot get any results. I have a loading scene (Scene 0) with this script attached to an empty game object.

function OnLevelWasLoaded (level : int) { 
    if (level == 3) { 
        print("level 3 loaded"); 
    } 

}

There are a total of 6 scenes in the project and they are loaded into the Build Settings, so there is a Scene 3.

When I click play or build it I get absolutely no results on a couple of different machines. Am I missing some other part?

thanks

If you’re loading scene 0, then “level” can’t equal 3, so nothing will be printed. That script would only work if you’re loading level 3. Only one level at a time can be loaded.

–Eric

I’m obviously missing something conceptually…

I thought the idea was to put this script into a scene and it would tell you when other scene(s) in the game were loaded, and ready to be accessed. This is not the case?

The docs also lead me to believe that “scene” was the same as “level” as far as this was concerned, but it sounds like perhaps that’s not the case either?

Any light you can shed is appreciated.

Scenes and levels are the same thing. OnLevelWasLoaded fires once when a level is loaded. If you have a level management script, it would have to use DontDestroyOnLoad or else it won’t exist in any other levels.

–Eric

I changed the script so that the level number was the same as the scene number of the scene the script is in and I still get nothing. It won’t print the message.

Also, is there a standard method for loading another level and waiting for it to load before leaving the current scene?

thanks

It won’t print anything if the level is already loaded. You’d have to start the game and load a different level.

Application.LoadLevel. You don’t have to wait, since it loads the level in one frame, regardless of how long it takes.

–Eric

OK, I got OnLevelWasLoaded to work when jumping from one scene to the next. How this is different from using function Awake()?

Also, let me rephrase the other question. If I’m in Scene 0, and I have a Loading… animation playing and I want to stay in Scene 0 looking at it until the data for scene 1 is all ready to play, what would be the process? Does this require a streamed web player?

Kevin

Awake() fires once any time you instantiate an object that has a script with an Awake function. Therefore it’s not useful for a level managing script. OnLevelWasLoaded fires whenever a level is loaded.

Streamed web player, or Application.LoadLevelAsync (requires Pro).

–Eric

Got it.

Thanks, Eric.

Hi Eric! I’m having a problem with OnLevelWasLoaded(level : int) related to what you said above. My level manager is marked DontDestroyOnLoad and therefore persists across all level loads just fine, and when I load level one my OnLevelWasLoaded event handler fires perfectly and the tasks designated for it execute without a hitch.

Nevertheless, I need to execute some actions on level 0, with the caveat that in my game I can go back to level 0 after loading levels 1, 2, 3, etc. (think “return to Intro” from anywhere in the game), so therefore I cannot execute them in Awake() nor Start() because I need to go through those steps every time level 0 is loaded and DontDestroyOnLoad scripts only go through Awake Start once. I thought OnLevelWasLoaded(level : int) would be the perfect place to put those actions, after trapping the value of 0 for the argument passed in, of course… but it turns out that, apparently, OnLevelWasLoaded does not fire the first time level 0 is loaded, i.e. when the game first opens (the lack of a simple Debug.Log() executing at the very top of OnLevelWasLoaded proves this to me). It may fire when I go back to level 0, maybe, haven’t tested yet, but I do need it to fire the first time-round because otherwise a lot of necessary things will not execute.

So, have you seen this? Have you seen OnLevelWasLoaded not firing the first time level 0 is loaded? I could of course place the actions I need both in Start() and in OnLevelWasLoaded for when the game goes back to level 0, hoping the event handler does fire then, but I’d certainly love to know if I’m doing things right or if there is a cleaner solution.

Tks!

  • jmpp

A bit of further investigation tells me that, indeed, OnLevelWasLoaded does not fire when level 0 is first loaded, i.e. when the game just opens:

http://www.unifycommunity.com/wiki/index.php?title=Life_cycle#.5B0_:_OnLevelWasLoaded.5D

(nevertheless, I’m not entirely sure about the accuracy of that article, so having confirmation would be great!)

So, apparently, the only option is to place my code both in Start and OnLevelWasLoaded for level 0, to cover both cases I need. I just tested and confirmed this approach does work.

Still, any extra light shed on the issue would be most appreciated! Regards,

  • jmpp

I just had an issue that I consider to be bug-like, but it could be intentional behavior. It might help you here.

It seems that even though you use DontDestroyOnLoad with a GameObject, any script attached to that object will be disabled, then re-enabled.

So that means you could put your code in OnEnable, and it will be called every time you load a level, even though the GameObject persists. If you only want the code to be called for certain levels, just check for the desired level index.

Hey God at Play! Thanks for the comment. I’ve noted that behaviour too and do consider it a tad annoying, having had to circumvent it a couple of times already in some of my DontDestroyOnLoad scripts to keep them from acting funny (but certainly still not having resolved the problem entirely satisfactorily).

I did, however, find a rather clean solution to my original problem, which was simply to wrap up the set of instructions that I need to have always executed upon loading level 0 in a “delegate” method that I call both in Start() and in OnLevelWasLoaded(level : int) after detecting the condition level == 0.

Hope that helps others with the same problem! Regards,

  • jmpp