OnLevelWasLoaded called before Awake?

We have some scripts that do quite a bit when we load a new scene (spawning players who had previously joined the game, etc.). One thing that we had been assuming was that Awake() would be called on the script before anything else could be, but we're getting OnLevelWasLoaded() calls before Awake().

  1. Is this the way it's supposed to work, or is it some strange side-effect of our test environment (normally we test a non-editor client and an in-editor server on the same machine)?

  2. Since we don't get the OnLevelWasLoaded() call when starting up from a given level, we call some initialization stuff both from there and from Awake(), with verification checks to see if we've already initialized. Which feels kind of gross; is there a better way? Is this a circumstance in which we should just do the initialization in the constructor?

Actually, I ran into the same bug and can confirm that it is not happening in the documented order. For every object in the new scene's hierarchy, OnLevelWasLoaded() gets called BEFORE those objects' Awake().

Here's the kicker: I was able to make it work the documented way by adding an OnEnable() function to the script! If this function exists, the methods get called in the documented order. If it doesn't, they don't. At least that's what I've found so far.

MOD EDIT: This post shows up on top of Google, so I’m editing this for future reference, as it’s outdated.
In Unity 5, Awake is called before OnLevelWasLoaded. This has been tested in 5.2.0f3.

Please read this http://www.unifycommunity.com/wiki/index.php?title=Event_Execution_Order

Unity's doc on this http://unity3d.com/support/documentation/Manual/Execution%20Order.html

I use OnLevelWasLoaded to get rid of very annoying lag at level start up. This happens whenever a new level is started that is heavy enough to cause that. I start Updates only after some boolean in OnLevelWasLoaded became true. I think OnLevelWasLoaded is related to assets loaded with Application.LoadLevel. I suspect you're loading assets (GameObjects etc) not at once with LoadLevel but later with some other procedure. Whenever a Gameobject is loaded (set active), Awake() is called on it. Means you can have Awakes even during your game already playing because you loaded a new object. Thus you have Awakes after OnLevelWasLoaded.

Awake is used to initialize any variables or game state before the game starts. Awake is called only once during the lifetime of the script instance. Awake is called after all objects are initialized so you can safely speak to other objects or query them using eg. GameObject.FindWithTag. Each GameObject's Awake is called in a random order between objects. Because of this, you should use Awake to set up references between scripts, and use Start to pass any information back and forth. Awake is always called before any Start functions. This allows you to order initialization of scripts. Awake can not act as a coroutine.

If you add DontDestroyOnLoad Awake and Start will not be called again.

After both Awake and Start have been called on every object in the scene, OnLevelWasLoaded will be run on every active object.

If you're getting OnLevelLoad calls at all before Awake and Start that might be a bug o.O or some weird serverlag, since you mentioned it's in a networking project.

Quick update that this function has been deprecated, and the new SceneManager sceneLoaded delegate should be used instead;

        void Start ()
        {
            SceneManager.sceneLoaded += OnSceneLoad;
        }

        private void OnSceneLoad (Scene arg0, LoadSceneMode arg1)
        {
           //DO something
        }

OnLevelWasLoaded called after Awake, There is an example below:

if u use this kind of code then it will work only when u come into level 0 from any other level.
void OnLevelWasLoaded(int level) {
if (level == 0)
Debug.Log(“Do Something”);
}

if you run directly level 0 it will not work.