Unity3D MonoBehaviour Lifecycle

August 2014 edit: UTech now have their own, more complete version of this diagram in the Unity Manual.

When you’re working with Unity3D, there are many different hooks you can hang your code on, and it’s often helpful to know which hooks are going to be called at what times.

I did some experimentation to figure out exactly when a bunch of the most important things happen, and put the information together into this handy infographic, which people appear to find useful:

My experiments were done under Unity 3.4, but as far as I know nothing has changed for 3.5 or 4.x.

The only bit of info I’d add that I’ve discovered since is that Awake doesn’t get called until the GameObject that the MonoBehaviour is attached to becomes active. One almost always creates a MonoBehaviour on an active GameObject, causing Awake to be called immediately (i.e. during the Instantiate call), but be aware of it if you’re doing something unusual (like adding components to Prefabs, which are permanently inactive).

If there’s other lifecycle questions that this infographic doesn’t answer clearly enough, post them in the comments and I’ll see about getting them added in sometime.

Category: Technology | Tags: 10 comments »

10 Responses to “Unity3D MonoBehaviour Lifecycle”

  1. Ben Pitt

    Excellent diagram! Although a tiny discrepency (as I just tweeted to you :) The diagram currently implies that FixedUpdate is called at least once every update cycle. However it is possible for FixedUpdate to not be called at all during an Update, if the game FPS is running faster than the physics timestep. FixedUpdate is run one-or-more times only if the physics simulation has fallen behind the current frame time by one or more physics timesteps. If the game is running at 100fps, and the physics timestep is set to 0.04, you’ll only get a FixedUpdate call on average every four Updates

  2. Dented Pixel

    Great Graphic, I discovered most of this independently when creating my framerate monitor FPS Graph http://u3d.as/content/dented-pixel/fps-graph-performance-analyzer/400 . It could have saved me a lot of time, if I saw this link first!

    On thing to add: there is also OnPostRender which is called around the same time as OnGUI (but helpfully it is only called once).

  3. Waiting for Simulation results in Unity3d using Coroutines | Programming and Stuff

    [...] The method StartAlgorithm() first resets the _collided counter (a public member of Master) and then generates the n Slaves at random positions above the plane. Now that we have initialized the simulation, we can start the algorithm itself. This is done with the StartCoroutine(Algorithm()) call. The important thing is that StartCoroutine will return without executing the Algorithm method itself. The coroutine is added to a collection of coroutines that the engine will execute at a later point in time (after Update see MonoBehaviour Lifecycle). [...]

  4. Martin Pernica

    Nice! This will be really useful for my students, thank you.

  5. Khan-amil

    One thing I found recently which is useful in editor, but not exactly in a “lifecycle”, is the “OnValidate” message, that get sent to the component each time you modify one of its values in the inspector.

  6. Walt D

    An important gotcha/inconisistency to note that this chart doesn’t make clear is that, during startup, Awake() for all objects is *not* called before OnEnable() for all objects. The pseudocode for the actual order looks like this:

    foreach obj in theGame{
    obj.Awake()
    if obj is enabled{
    obj.OnEnable()
    }
    }
    foreach obj in the theGame{
    if obj is enabled{
    obj.Start()
    }
    }

  7. Walt D

    Additionally, Start() *does* get called for instantiated objects, but not until immediately before the instantiated object first updates itself, which could be (and probably is) *after* other objects get updated. So unfortunately you can’t assume that an instantiated objects will be Start()’ed at the beginning of the next “Updates” phase — only that it will be Start()’ed by the time it updates itself. See this thread for details: http://forum.unity3d.com/threads/32070-Start-before-Update-except-when

  8. Adam Brockway

    Can you share what you are using this for? Any examples?

  9. Khan-amil

    Well, I don’t have particular examples in mind, but it’s usefull to safety-check values exposed for a designer to modify in a prefab.
    Most times it can save you fromm writing an entire custom inspector for just one value or two.
    Lately I’ve used it to speed up setup in a scene : whenever the component detected that I changed a reference to an object it had during OnValidate, it changed other parameters, then created an object to draw a line beetween it and the new object referenced.

  10. Jan Rabe

    Well you can always have a look at http://docs.unity3d.com/ScriptReference/MonoBehaviour.html :)


Leave a Reply



Back to top