Skip to main content

💥 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 OnCollisionEnter for solid hits
  • Use OnTriggerEnter for 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);
}
MethodFires whenParameter tells you
OnCollisionEntersolid objects bumpthe Collision (has .gameObject)
OnTriggerEntersomething enters a triggerthe other Collider (has .gameObject)
💡 Also available: ...Stay (every frame while touching) and ...Exit (when they separate). Enter is the one you'll use most.

Tags: Knowing What You Hit

Usually you only care about certain collisions — "did the Player touch this?" A tag is a label you give a GameObject so code can recognize it:

  1. Select the player. At the top of the Inspector, open the Tag dropdown and choose Player (a built-in tag), or Add Tag… to make your own.
  2. In code, check it with CompareTag:
void OnTriggerEnter(Collider other)
{
    if (other.CompareTag("Player"))
    {
        Debug.Log("The player entered!");
    }
}

⚠️ Use CompareTag, not ==

Write other.CompareTag("Player"), not other.tag == "Player". CompareTag is faster and won't throw if the tag is unset. Also — tags are case-sensitive and must exist in the Tag list first.

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.gameObject means "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)
        }
    }
}
graph LR A[Player enters coin's trigger] --> B{Is it tagged Player?} B -->|yes| C[Log + Destroy the coin] B -->|no| D[Ignore]

Hands-on Exercise

🏋️ Exercise: First pickup

  1. Tag your player object as Player. Give it a Rigidbody (needed for triggers).
  2. Add a small Cube as a coin. Tick Is Trigger on its collider.
  3. Put the Coin script above on the coin.
  4. 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

  • OnCollisionEnter handles solid bumps; OnTriggerEnter handles trigger overlaps.
  • Use tags + CompareTag("Player") to react only to the right objects.
  • Destroy(gameObject) removes an object; mind gameObject vs other.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.

🎉 Your code reacts to the world!