CodeBlanks Logo Icon CodeBlanks

Pickup mechanic

Write a pickup script for collectible items.

Pickup Mechanism and Triggers

Collectibles such as coins, potions, or keys must allow the player to pass through rather than stopping movement like a solid wall. For this, Is Trigger is enabled on the Collider component.

// Trigger-enabled colliders report overlaps rather than solid impacts
void OnTriggerEnter(Collider other) {
    if (other.CompareTag("Player")) {
        // Execute collection logic
    }
}

This disables rigid physical collision while allowing overlap events to be processed via code.

Collection Event and Destroy

When the player intersects the coin, OnTriggerEnter executes. The player tag is confirmed, score is credited, and the collected item is purged using Destroy(gameObject).

void OnTriggerEnter(Collider other) {
    if (other.CompareTag("Player")) {
        // Increment score and delete coin from scene
        Destroy(gameObject);
    }
}

Optionally, a particle burst or audio cue is played at the pickup position for player feedback.

Practice Coin pickup notification

Define a Start method. Inside, declare int altinDegeri = 10; and print "Toplanan altin: " + altinDegeri to the console.

Starter code

Player.cs
using UnityEngine;

public class PickupTester : MonoBehaviour
{
    // Start icinde altinDegeri tanimla ve yazdir
}

Sample solution

Player.cs
using UnityEngine;

public class PickupTester : MonoBehaviour
{
    void Start()
    {
        int altinDegeri = 10;
        Debug.Log("Toplanan altin: " + altinDegeri);
    }
}

Expected output

Toplanan altin: 10

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which setting must be enabled on a Collider so a collectible coin can be passed through instead of blocking the player?

  • Cast Shadows
  • Is Kinematic
  • Static Mesh
  • Is Trigger Correct answer

Explanation: Enabling 'Is Trigger' on a Collider removes physical obstruction, allowing characters to overlap and trigger events.

2 Destroy collected game object

Question
Destroy(____);
Answer
Destroy(gameObject);

Explanation: To remove the entire object hosting the script from the scene, 'Destroy(gameObject)' is called.

3 Pickup mechanism concepts

Term Meaning
Is Trigger Enables trigger overlap, no collision
OnTriggerEnter Fires when object enters trigger volume
CompareTag Safely verifies the tag of an object
Destroy Removes the object from scene and memory

All lessons in Advanced