Methods and the game loop
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.
Awake and Start run once at birth. Update runs again and again, roughly 60 times a second.
The three you will write every day
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.
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
transform.position += Vector3.forward * 3f;✓ 3 units per second
transform.position += Vector3.forward * 3f * Time.deltaTime;Writing your own method
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
}