COIT20271 — Mobile Game Development
Week 10
Knowledge Revision
Review these concepts before attempting the Knowledge Check quiz.
MonoBehaviour Lifecycle
Awake()
Called when the script
instance is loaded,
even if disabled
▶
OnEnable()
Called when the
object becomes active
▶
Start()
Called once before
the first Update,
only if enabled
▶
FixedUpdate()
Fixed interval (0.02s)
Use for physics
& Rigidbody forces
▶
Update()
Every frame
Use for input,
non-physics logic
▶
LateUpdate()
After all Update
calls. Use for
camera follow
Key Points to Memorise�
• Awake() runs even if the script component is disabled. Start() waits until enabled.�
• FixedUpdate() is for Rigidbody/physics work. Update() is for everything else.�
• Applying forces in Update() causes jittery physics — always use FixedUpdate().�
• LateUpdate() runs after all Update() calls — ideal for camera follow scripts.
GetComponent & References
Getting References
// Cache in Start or Awake
Rigidbody rb;
void Awake() {
rb = GetComponent<Rigidbody>();
}
Returns null if the component is not attached → NullReferenceException if you use it.
[SerializeField] vs public
| Inspector | Other Scripts |
public | ✅ Visible | ✅ Accessible |
[SerializeField] private | ✅ Visible | ❌ Blocked |
Common Bug: Unassigned Inspector References�
A public or [SerializeField] field that you forget to drag-assign in the Inspector defaults to null.�
Accessing any property on it (e.g., player.transform) throws NullReferenceException at runtime.
Collisions vs Triggers
Physical Collision
Is Trigger: OFF (unchecked)�
Objects physically bounce/push each other.�
�Callback:�
OnCollisionEnter(Collision col)
Trigger (Pass-Through)
Is Trigger: ON (checked)�
Objects pass through — detection zone only.�
�Callback:�
OnTriggerEnter(Collider other)
Both require: Both objects must have Colliders. At least one must have a Rigidbody.
Destroy(other) removes the Collider only. Use Destroy(other.gameObject) to remove the whole object.
Destroy & Instantiate
Destroy()
Destroy(gameObject); // Immediate
Destroy(gameObject, 3f); // After 3 seconds
Destroy(other); // Component only!
Destroy(other.gameObject); // Whole object
• The delay is in seconds, not frames.�
• Object stays active during the delay.�
• Destroying a Component ≠ destroying the GameObject.
Instantiate()
GameObject clone =
Instantiate(prefab,
position,
rotation);
• Returns a reference to the new clone.�
• The prefab field must be assigned in the Inspector.�
• Quaternion.identity = no rotation.
Coroutines — Spawning with Delays
IEnumerator SpawnLoop() {
while (true) {
Instantiate(prefab, pos, Quaternion.identity);
yield return new WaitForSeconds(2f); // Pauses THIS coroutine only
}
}
void Start() { StartCoroutine(SpawnLoop()); }
Input Handling
Method | Returns true... | Use case |
GetKey() | Every frame the key is held | Continuous movement, firing |
GetKeyDown() | Only the first frame pressed | Jump, single action, menu toggle |
GetKeyUp() | Only the frame it is released | Charge release, throw |
Input.GetAxis()
Returns a float from -1 to 1 with smoothing.��
"Horizontal" → A/Left = -1, D/Right = +1�
"Vertical" → S/Down = -1, W/Up = +1��
GetAxisRaw() returns -1, 0, or 1 with no smoothing.
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 move = new Vector3(h, 0, v);
rb.velocity = move * speed;
XZ plane movement — Y stays at 0.
Core Unity Concepts
Prefab
A saved GameObject template stored as an asset. Instantiate clones at runtime. Changes to the Prefab propagate to all instances.
Canvas
Required root parent for all UI elements (Text, Button, Image). Unity also needs an EventSystem for interactive elements.
Time.deltaTime
Seconds since last frame. Multiply speed values by this to get frame-rate independent movement (units per second, not per frame).
CompareTag()
Safer than tag == "string". Throws an exception if the tag doesn't exist in the Tag Manager — catches typos early.
Methods You Must Know
Method / Property | What It Does |
GetComponent<T>() | Get a component on the same GameObject |
Destroy(obj) | Remove a GameObject or component |
Destroy(obj, delay) | Remove after delay seconds |
Instantiate(prefab, pos, rot) | Clone a prefab at a position |
StartCoroutine(method()) | Begin a coroutine |
Input.GetAxis("name") | Smoothed axis input (-1 to 1) |
Input.GetKeyDown(KeyCode.X) | True only on the first frame pressed |
CompareTag("tag") | Safe tag check (throws if tag missing) |
transform.Translate(dir) | Move relative to local axes |
Time.deltaTime | Seconds since last frame |