Share via


Game Loop - Awake, Start, Update

Introduction

The central component of any game, from a programming standpoint, is the **game loop. **It allows the game to run smoothly regardless of a user's input or lack thereof.

Every game must and should have a game loop because a game must continue regardless of a user's input. In this article I will be talking about two important event functions in Unity3D i.e., Awake() and Start().

http://onestopdotnet.files.wordpress.com/2014/04/game-loop1.jpg

 

Basics of scripting

First, you need to understand what is Unity 3D and the importance of scripting (C#/UnityScript) in game development. To do this you have to visit my technical blog where I blogged the important aspects and concepts of unity2D/3D game development.

Learn Gaming

Time to Awake() and Start()

By now you should understand what are the main components and aspects of unity2D/3D game development. Just to quickly recall, in Unity2D/3D

  • 1 Project = 1 Game
  • 1 Level = 1 Scene

Awake and Start are two functions that are called automatically when a script is loaded.

When a scene starts, Awake() is the function which is always called (once for each object in the scene) before any Start functions. But there are some points to remember:

  • Awake() is called only after a prefab is instantiated.
  • If a GameObject is in-active during start up Awake is not called until it is made active, or a function in any script attached to it is called.
  • Awake is called first even if the script component is not enabled and is best used for setting up any resources between scripts and initialization.

**Start() **is called before the first frame update only if the script instance is enabled.

  • Start is called after Awake,immediately before the first Update,but only if the script component is enabled.
  • This means that you can use Start for anything you need to occur when the script component is enabled. This allows you to delay any part of your initialization code until it's really needed.

** Using the code(C#)**

01.// Loading required Assemblies02. 03.using UnityEngine;04. 05.using System.Collections;06. 07.  08. 09.public class AwakeAndStart : MonoBehaviour10. 11.{12. 13.//Awake is called first even if the script component is not enabled and is best used for setting up any   resources between scripts and initialization. 14. 15.  16. 17.    void Awake ()18. 19.    {20. 21.        Debug.Log("Awake called.");22. 23.    }24. 25.     26. 27.//Start is called after Awake,immediately before the first Update,but only if the script component is enabled.  28. 29.  30. 31.    void Start ()32. 33.    {34. 35.        Debug.Log("Start called.");36. 37.    }38. 39.}

Time for Update() and FixedUpdate()

When you're keeping track of game logic and interactions, animations, camera positions, etc., there are a few different events you can use. The common pattern is to perform most tasks inside the Update() function, but there are also other functions you can use.

**Update **is the most commonly used function in Unity.It's called once per frame on every script that uses it. Almost anything that needs to be changed or adjusted happens here.

  • Called every frame
  • Used for regular updates such as:
    • Moving Non-Physics objects
    • Simple Timers
    • Receiving Input
    • Update interval times vary

Note that Update is not called on a regular timeline. If one frame takes longer to process then the time between update calls will be different.

FixedUpdate is a similar function to update but it has a few important differences. FixedUpdate() is often called more frequently than Update(). It can be called multiple times per frame, if the frame rate is low and it may not be called between frames at all if the frame rate is high. Fixed Update is called on a regular timeline and will have same time between calls. Immediately after FixedUpdate is called any necessary physics calculations are made. As such anything that affects a rigid body-meaning a physics object-should be executed in fixed update rather than update.

In short:

  • Called Every Physics Step
  • FixedUpdate intervals are consistent
  • Used for regular updates such as adjusting physics (Rigibody) objects

If you are planning to change the state of a physics
GameObject-FixedUpdate()
Non-Physics GameObject-Update()

Using the code (C#)

01.//Run this code in UnityEditor and you will understand the differences. 
02. 
03.using UnityEngine;
04. 
05.using System.Collections;
06. 
07.  
08. 
09.public class  UpdateAndFixedUpdate : MonoBehaviour
10. 
11.{
12. 
13.//Logs a regular time interval say :0.02s
14. 
15.    void FixedUpdate ()
16. 
17.    {
18. 
19.        Debug.Log("FixedUpdate time :" + Time.deltaTime);
20. 
21.    }
22. 
23.     
24. 
25.//Logs inconsistent time intervals  
26. 
27.    void Update ()
28. 
29.    {
30. 
31.        Debug.Log("Update time :" + Time.deltaTime);
32. 
33.    }
34. 
35.}

See Also