💥 Lesson 5.3: Handling Collisions in Code
Detecting a bump or an overlap is only step one — now let's make code respond. This is where "the player touched the coin" becomes "collect it and add a point."
🎯 Learning Objectives
- Use
OnCollisionEnterfor solid hits - Use
OnTriggerEnterfor trigger overlaps - Identify what you hit using tags and
CompareTag - Remove an object with
Destroy
Estimated Time: 40 minutes
In This Lesson
The Event Methods
Unity automatically calls special methods on your script when a collision or trigger happens. You just write the method and Unity does the rest:
// Called when a SOLID collision begins
void OnCollisionEnter(Collision collision)
{
Debug.Log("Bumped into " + collision.gameObject.name);
}
// Called when a TRIGGER overlap begins
void OnTriggerEnter(Collider other)
{
Debug.Log("Overlapped with " + other.gameObject.name);
}
| Method | Fires when | Parameter tells you |
|---|---|---|
OnCollisionEnter | solid objects bump | the Collision (has .gameObject) |
OnTriggerEnter | something enters a trigger | the other Collider (has .gameObject) |
💡 Also available:...Stay(every frame while touching) and...Exit(when they separate).Enteris the one you'll use most.
Destroying Objects
Destroy removes a GameObject from the scene — perfect for a collected coin or a defeated enemy:
Destroy(other.gameObject); // remove the object we overlapped
Destroy(gameObject); // remove THIS object (the script's own GameObject)
💡 gameObject vs other.gameObject:gameObject(lowercase) means "the object this script is on."other.gameObjectmeans "the object we just touched." Destroying the wrong one is a common mix-up — read carefully.
Putting It Together
Here's a script for a coin that vanishes when the player walks into it. Put it on the coin (whose collider has Is Trigger ON):
using UnityEngine;
public class Coin : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
Debug.Log("Coin collected!");
Destroy(gameObject); // remove the coin (this object)
}
}
}
Hands-on Exercise
🏋️ Exercise: First pickup
- Tag your player object as Player. Give it a Rigidbody (needed for triggers).
- Add a small Cube as a coin. Tick Is Trigger on its collider.
- Put the
Coinscript above on the coin. - Press Play, walk the player into the coin — it disappears and the Console prints "Coin collected!".
💡 Nothing happens when I touch it
Checklist: (1) coin's collider has Is Trigger ON, (2) the player has a Rigidbody, (3) the player is tagged exactly "Player", (4) the script is on the coin. Missing any one is the usual cause.
🎯 Quick Quiz
Question 1: Which method fires when something enters a trigger zone?
Question 2: In the Coin script, what does Destroy(gameObject) remove?
Summary
🎉 Key Takeaways
OnCollisionEnterhandles solid bumps;OnTriggerEnterhandles trigger overlaps.- Use tags +
CompareTag("Player")to react only to the right objects. Destroy(gameObject)removes an object; mindgameObjectvsother.gameObject.- A pickup = trigger collider + OnTriggerEnter + tag check + Destroy.
🚀 What's Next?
You've built the mechanics of a pickup. Lesson 5.4 polishes it into a proper collectible — spinning, tagged, and ready for a score.