0
0
Unityframework~5 mins

Start and Update methods in Unity

Choose your learning style9 modes available
Introduction

The Start and Update methods help your game objects do things when the game begins and keep doing things every frame.

When you want to set up something once before the game runs, like setting a player's health.
When you want to check for player input continuously, like moving a character.
When you want to update the game score or timer every moment.
When you want to initialize variables or settings at the start of a scene.
When you want to animate or move objects smoothly during the game.
Syntax
Unity
void Start() {
    // code here runs once at the beginning
}

void Update() {
    // code here runs every frame
}

Start runs once before the first frame.

Update runs many times per second, once every frame.

Examples
This prints a message once at the start, then prints every frame.
Unity
void Start() {
    Debug.Log("Game started!");
}

void Update() {
    Debug.Log("Frame updated");
}
Sets player health once, then checks every frame if space is pressed to jump.
Unity
int playerHealth;

void Start() {
    playerHealth = 100;
}

void Update() {
    if (Input.GetKeyDown(KeyCode.Space)) {
        Jump();
    }
}
Sample Program

This program prints a message once when the game starts. Then every second (assuming 60 frames per second), it prints how many seconds have passed.

Unity
using UnityEngine;

public class Example : MonoBehaviour {
    int counter = 0;

    void Start() {
        Debug.Log("Start method called");
    }

    void Update() {
        counter++;
        if (counter % 60 == 0) {
            Debug.Log($"Update called {counter / 60} seconds");
        }
    }
}
OutputSuccess
Important Notes

Start is good for setup tasks.

Update is good for things that change over time or need checking often.

Too much work in Update can slow your game, so keep it simple.

Summary

Start runs once at the beginning.

Update runs every frame to keep things moving.

Use them to control when and how your game objects act.