x


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?

more ▼

asked Apr 21 '11 at 10:40 PM

Steve 9 gravatar image

Steve 9
150 2 4 11

Also: I hadn't noticed this before, but someone else on our team brought to my attention that we also sometimes get OnSerializeNetworkView() before Awake() calls!

May 03 '11 at 04:17 PM Steve 9
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered May 03 '11 at 11:58 AM

eheimburg gravatar image

eheimburg
214 9 12 17

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 21 '11 at 11:01 PM

synapsemassage gravatar image

synapsemassage
426 2 5 16

Thanks! That page looks helpful. But is it incorrect about the call order, or is my experience some special-case situation? We also get a Start() call after the Awake() call, which according to that page shouldn't happen at all during level load...

Apr 22 '11 at 12:01 AM Steve 9

...Or am I misinterpreting the page, and there isn't a reliable order between the two calls at all? It's really not clear at all.

Apr 22 '11 at 12:03 AM Steve 9

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 some other procedure.

Apr 22 '11 at 08:37 AM synapsemassage

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.

Apr 22 '11 at 08:37 AM synapsemassage

Thus you have Awakes after OnLevelWasLoaded.

Apr 22 '11 at 08:38 AM synapsemassage
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 22 '11 at 12:46 AM

Joshua gravatar image

Joshua
6.4k 19 25 70

Thank you. Any ideas on how lag could cause this? I'd expect that, if anything, I'd be seeing the OnLevelWasLoaded later than normal in a networked game...

Apr 22 '11 at 06:57 PM Steve 9
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x55
x40
x4

asked: Apr 21 '11 at 10:40 PM

Seen: 3343 times

Last Updated: Apr 21 '11 at 10:40 PM