Tags: unity, csharp, monobehaviour, coroutines, physics, raycasting, serialization, game-engine Last updated: 2026-06-27

Unity (C#) Cheatsheet

Quick Reference

ConceptKey API
LifecycleAwake() / Start() / Update() / FixedUpdate()
CoroutinesStartCoroutine() / yield return / WaitForSeconds
PhysicsRigidbody / Collider / OnCollisionEnter
RaycastingPhysics.Raycast() / Physics2D.Raycast()
InputInput.GetAxis() / Input.GetKeyDown()
Serialization[SerializeField] / [HideInInspector] / ScriptableObject
DebugDebug.Log() / Debug.DrawLine() / Gizmos
ScenesSceneManager.LoadScene() / DontDestroyOnLoad()

MonoBehaviour Lifecycle

Awake()          → Called once when the object is created
OnEnable()       → Called when object becomes active
Start()          → Called before first Update
FixedUpdate()    → Physics tick (0.02s default)
Update()         → Every frame
LateUpdate()     → After all Update() calls finish
OnDisable()      → When object becomes inactive
OnDestroy()      → When object is destroyed

Execution Order

// Awake — before Start. Best for self-reference.
void Awake() { rb = GetComponent<Rigidbody>(); }

// Start — after Awake. Good for referencing other objects.
void Start() { player = GameObject.FindWithTag("Player"); }

// Update — every frame. Input and movement.
void Update() {
    float h = Input.GetAxis("Horizontal");
    transform.Translate(h * speed * Time.deltaTime, 0, 0);
}

// FixedUpdate — consistent physics step.
void FixedUpdate() { rb.AddForce(Vector3.down * gravity); }

// LateUpdate — after Update. Camera follow, aiming.
void LateUpdate() { cam.transform.position = target.position + offset; }

Coroutines

// Start
StartCoroutine(MyCoroutine());
StartCoroutine("MyCoroutine");  // string-based (stoppable by name)

// Yield instructions
yield return null;                       // Wait one frame
yield return new WaitForSeconds(2f);     // Wait 2s
yield return new WaitForEndOfFrame();    // After frame render
yield return new WaitUntil(() => hp <= 0);
yield return new WaitWhile(() => isPaused);

// Stop
StopCoroutine("MyCoroutine");
StopAllCoroutines();

// Spawn waves
IEnumerator SpawnWave(int count, float interval) {
    for (int i = 0; i < count; i++) {
        Instantiate(enemyPrefab, transform.position, Quaternion.identity);
        yield return new WaitForSeconds(interval);
    }
}

Physics

Rigidbody & Colliders

Rigidbody rb = GetComponent<Rigidbody>();
Collider col = GetComponent<Collider>();

rb.mass = 10f;
rb.drag = 1f;
rb.useGravity = true;
rb.isKinematic = true;      // Not affected by forces
rb.constraints = RigidbodyConstraints.FreezeRotationZ;

// Physics-based movement
rb.velocity = new Vector3(h * speed, rb.velocity.y, v * speed);
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
rb.MovePosition(transform.position + direction * Time.fixedDeltaTime);

Collision & Trigger Detection

// Collision (physical response)
void OnCollisionEnter(Collision col) {
    if (col.gameObject.CompareTag("Enemy")) TakeDamage();
}

// Trigger (collider set to "Is Trigger")
void OnTriggerEnter(Collider other) {
    if (other.CompareTag("Pickup")) Collect(other.gameObject);
}

Raycasting

// 3D
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 100f)) {
    Debug.Log($"Hit: {hit.collider.name} at {hit.point}");
}

// Layer mask
int layerMask = LayerMask.GetMask("Enemy", "Ground");
if (Physics.Raycast(origin, direction, out hit, maxDist, layerMask)) { }

// Sphere / Box cast
Physics.SphereCast(origin, radius, direction, out hit, maxDist);
Physics.BoxCast(center, halfExtents, direction, out hit, rotation, maxDist);

// 2D
RaycastHit2D hit2d = Physics2D.Raycast(origin, direction, distance, layerMask);

// Debug
Debug.DrawRay(origin, direction * distance, Color.red, 1f);

Serialization & ScriptableObject

[SerializeField] private int health = 100;
[HideInInspector] public float cachedValue;
[Range(0, 100)] public float speed = 10f;
[Tooltip("Maximum jump height in units")] public float jumpHeight = 2f;
[Header("Movement")]
public float walkSpeed = 5f;

// ScriptableObject — data container, no GameObject needed
[CreateAssetMenu(fileName = "NewWeapon", menuName = "Game/Weapon")]
public class WeaponData : ScriptableObject {
    public string weaponName;
    public int damage;
    public Sprite icon;
    public GameObject prefab;
}

// JsonUtility
string json = JsonUtility.ToJson(playerData);
PlayerData loaded = JsonUtility.FromJson<PlayerData>(json);

Tips