U
tab 01Basics11 lessons — tap to open
02 / 11 · 5 min
Basics

Methods and the game loop

your project so far

Still no project. You are learning the three methods Unity calls for you — you will watch them run from lesson 06 on.

A method is a named block of code you can run. Unity runs three of them for you automatically.

animated diagram

Awake and Start run once at birth. Update runs again and again, roughly 60 times a second.

The three you will write every day

Lifecycle.cs
void Awake()  { /* set up my own references */ }
void Start()  { /* everything else exists now */ }
void Update() { /* runs every frame */ }
  • void means the method gives nothing back. The ( ) holds the inputs — empty here.

  • Awake fires before Start, on every object, so Start can safely use other objects.

  • Update is your game loop: input, movement, timers, checks.

Time.deltaTime — the one rule of Update

A fast PC calls Update more often than a slow phone. Multiplying by Time.deltaTime (the seconds since the last frame) makes movement the same speed everywhere.

animated diagram

Every box is one Update() call. The top half adds 5 HP per call, so the fast machine lands on 50 HP when the second is over; the bottom half adds 5 HP × deltaTime, so both lanes stop on the same 5 HP.

✖ 3 units per frame

frame-dependent
transform.position += Vector3.forward * 3f;

✓ 3 units per second

frame-independent
transform.position += Vector3.forward * 3f * Time.deltaTime;

Writing your own method

Damage.cs
int hp = 100;

void Update()
{
    if (Input.GetKeyDown(KeyCode.Space))
        TakeDamage(20);            // call it
}

void TakeDamage(int amount)        // amount = the input
{
    hp -= amount;
    Debug.Log("HP left: " + hp);   // prints to the Console
}