Click here to Skip to main content
Click here to Skip to main content

Unity and C#: Game Loop (Awake, Start, Update)

, 19 Apr 2014
Rate this:
Please Sign up or sign in to vote.
Unity and C#: 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 tip, I will be talking about two important event functions in unity3D, i.e., Awake() and Start().

Basics of Scripting

Firstly, you need to understand what is unity3D 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 have understood 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#)

using UnityEngine;

using System.Collections; 

public class AwakeAndStart : MonoBehaviour
{
//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. 

    void Awake ()
    {
        Debug.Log("Awake called.");
    }
    
//Start is called after Awake, immediately before the first Update, but only if the script component is enabled.  
 
    void Start ()
    {
        Debug.Log("Start called.");
    }
} 

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. FixedUpdate is called on a regular timeline and will have the same time between calls. Immediately after FixedUpdate is called, any necessary physics calculations are made. As such, anything that effects a rigidbody-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#)

[code language="csharp"]//Run this code in UnityEditor and you will understand the differences. 
using UnityEngine;
using System.Collections;

public class UpdateAndFixedUpdate : MonoBehaviour
{
//Logs a regular time interval say :0.02s
    void FixedUpdate ()
    {
        Debug.Log("FixedUpdate time :" + Time.deltaTime);
    }
    
//Logs inconsistent time intervals  
    void Update ()
    {
        Debug.Log("Update time :" + Time.deltaTime);
    }
}[/code]

References

  • Script Reference: Awake
  • Script Reference: Start
  • Unity3d.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Vidyasagar MSC
Software Developer (Senior)
India India
Microsoft MVP (Games for Windows)
Developer | Blogger | Mentor | Gamer
Coder by profession | Gamer By Heart
Technical Blog : http://onestopdotnet.wordpress.com/
About me: http://about.me/mscvidyasagar
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 19 Apr 2014
Article Copyright 2014 by Vidyasagar MSC
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid