C# for Unity: Coroutines & Multiplayer Networking

Lesson 1 of 7

GameObjects, Components, and the MonoBehaviour Lifecycle

In Unity, everything in a scene is a GameObject, an empty container that gets its behavior from attached Components. A MonoBehaviour script is a component that hooks into Unity's lifecycle.

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 5f;

    void Start()
    {
        Debug.Log($"{gameObject.name} spawned");
    }

    void Update()
    {
        float move = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
        transform.Translate(move, 0, 0);
    }
}

The lifecycle

  • Awake() runs once, before Start(), even on a disabled component, use it to cache references.
  • Start() runs once, after every Awake() has run, right before the first frame.
  • Update() runs every frame, use Time.deltaTime so movement stays framerate-independent.
  • FixedUpdate() runs on a fixed timestep, use it for physics (Rigidbody forces).

Getting components

GetComponent<T>() looks up another component attached to the same GameObject:

Rigidbody rb = GetComponent<Rigidbody>();
rb.AddForce(Vector3.up * 10f);

📝 GameObjects & Lifecycle Quiz

Passing score: 70%
  1. 1.Which MonoBehaviour method is best for movement, since it runs every frame?

  2. 2.Multiplying movement by Time.____ keeps it framerate-independent.

  3. 3.Awake() can run even on a component that starts disabled.