Unity + C# Quick Reference
Everything this course uses, on one page. When you forget an API, this is faster than searching the web — and unlike the web, every line here is one you have actually typed this year.
| C# | What it does | Unity / exam notes |
|---|---|---|
int x = 5; | A whole number, 32 bits | CSP: the exam calls these numbers; binary is under the hood |
float y = 5.0f; | A decimal (the f is required) | Unity positions and speeds are floats |
bool hasAxe = false; | true or false — one bit of information | The exam: true/false, no third option |
string name = "Boss"; | Text in quotes | Tags are strings: CompareTag("Coin") |
[SerializeField] private float speed; | Private in code, editable in the Inspector | Every tuning knob you build uses this pattern |
// comment | Ignored by the compiler, read by humans | Your future self is the human |
Debug.Log("Health: " + health); | Print to the Console | The exam's DISPLAY(expression) |
if (health <= 0) { ... } else { ... } | Selection | Every state machine is built from these |
for (int i = 0; i < n; i++) { ... } | Counted iteration | The exam: REPEAT n TIMES — and its lists start at 1, yours start at 0 |
foreach (Card_data c in deck) { ... } | Walk a list | The exam: FOR EACH item IN aList |
while (alive) { ... } | Loop while a condition holds | The exam: REPEAT UNTIL (condition) |
void Heal(int amount) { ... } | A method — the exam calls it a procedure; amount is a parameter | Create PT requires a procedure with a parameter |
class Boss : MonoBehaviour { ... } | A class inheriting MonoBehaviour = a Unity script | Attach it to a GameObject to run |
Unity essentials
| Unity API | What it does | Where you used it |
|---|---|---|
void Start() | Runs once, first frame | Setup: find the player, save startPosition |
void Update() | Runs every frame — input, movement, checks | PlayerController, TurretController, EnemyController |
Input.GetAxis("Horizontal") | -1 to +1, smoothed — A/D or arrows | Movement (1.4) |
Input.GetKeyDown(KeyCode.Space) | True only on the frame the key goes down | Jump (6.1) |
Input.GetMouseButtonDown(0) | Left click, that frame | Attack trigger (1.6) |
transform.Translate(v) | Move by vector v, ignoring physics entirely | Your first movement (1.4) — replaced in 2.2 once it walked through walls |
rb.linearVelocity = v | Set how fast the body travels, and let physics do the moving. Units are per second, so it takes no Time.deltaTime. | The course's movement method from 2.2 onward. Unity 6 renamed this from velocity — tutorials written before then will not compile. |
rb.MovePosition(rb.position + v) | Drive the body to a target position along the path, rather than at a speed | Moving platforms and scripted set pieces. Stops at walls only on a Dynamic body — the docs recommend Kinematic, which is the one body type it ignores them on. |
transform.position | Where in the world (Vector3) | Distance checks |
Vector3.Distance(a, b) | Straight-line distance between points | Turret radius (3.1), chase (4.3) |
(a - b).normalized | Direction from b to a, length exactly 1 | Chase (4.3), aiming a projectile (3.2) |
Time.deltaTime | Seconds since last frame | Multiply motion by it: per-frame, not per-second |
Time.time | Seconds since the scene started | Cooldowns (3.3, 4.4) |
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse) | Push the rigidbody — one impulse beat | Jump (6.1) |
rb.gravityScale | 0 = float, 2 = fall; swap for game modes | Top-down vs cave (5.3), ladders (7.3) |
Instantiate(prefab, pos, rot) | Clone a prefab into the scene | Projectiles (3.2), card deals (10.2) |
Destroy(gameObject) | Remove an object | Coin pickup (2.1), projectile death |
OnTriggerEnter2D(Collider2D other) | Fires when a trigger overlaps | Coins, pickups, ladders |
OnCollisionEnter2D(Collision2D c) | Fires when solid colliders hit | Spikes, enemy attack |
other.CompareTag("Coin") | Identity check by tag | Every pickup pattern |
GameObject.FindWithTag("Player") | Locate an object by tag | Turret and enemy finding you |
SceneManager.LoadScene(n) — an int build index, or LoadScene("Platformer") by name | Swap scenes. The index overload is the one this project ships: SceneSwitch.cs holds a serialized int scene, so its value is really a position in Build Settings, not a name. | The cave (5.3), the death loop. Reorder Build Settings and every serialized index now points at a different scene — silently, with no error. |
SceneManager.GetActiveScene().name | Which scene is running | Deciding what kind of scene you are in (5.3) |
anim.SetBool("IsWalking", b) / SetInteger / SetTrigger | Drive the Animator | WalkDir 0 up / 1 side / 2 down |
sprite.flipX = b | Mirror a sprite | Face the walk direction |
Random.Range(0, n) | Random int 0..n-1 (or float version) | Shuffles (10.2c) |
DontDestroyOnLoad(gameObject) | Survive a scene change | StatManager carrying your coins |
Mathf.Min / Max / Abs / FloorToInt | Number helpers | Health cap (2.3), coin halving on death |
List<T> + .Add / .RemoveAt / .Count | Dynamic list | Card decks and hands (10.2) |
The five patterns that run the whole year
| Pattern | Shape | Seen in |
|---|---|---|
| Pickup | trigger → CompareTag → change state → Destroy | coins, axe, boots, ladders |
| Cooldown | if (Time.time >= last + cooldown) → act, set last = Time.time | turret fire, enemy damage |
| Scene awareness | read the scene name once in Start → set flags (gravity, movement style) | overworld ↔ cave ↔ ladder |
| State machine | flags + nested if/else → behavior per state | enemy patrol/chase/attack, boss phases |
| Singleton | static instance + Awake guard + DontDestroyOnLoad | StatManager (5.3), GameManager (cards) |
If you can rebuild those five from memory, you can rebuild this entire game — which is more or less the point of the semester.
Body Type decides whether the world can stop you.
Before you debug any movement or collision problem, read the Rigidbody 2D's
Body Type — not your movement code. Only a Dynamic
body is stopped by walls and only a Dynamic body produces
OnCollision*
messages against static colliders. A Kinematic body collides with
Dynamic bodies only, so it passes through every wall, floor and cave door in silence.
Triggers are the exception: a Kinematic body still fires
OnTrigger*, which is why coins keep working and the bug is so hard to see.
This project's Player is Dynamic all year, with Gravity Scale 0 in the top-down scenes.
On the AP exam, read the sheet. The exam does not test
C# — it ships its own pseudocode reference sheet (1-indexed lists,
REPEAT n TIMES, procedures). The full translation lives at
Exam Reference Sheet.
The muscle you built here transfers; the notation needs ten minutes
of practice.
Where this appears in the course
- Docs home — all 12 units
- Pacing Calendar — when each API shows up
- The Build — what ships each phase