Source file unity-csharp/ebook/CoinPickup.cs from the GDnD code examples. Download raw file

// Demonstrates a 2D trigger pickup for The Architecture of Play ebook.
// Referenced by: docs/ebook/GDnD-Architecture-of-Play.md
using UnityEngine;
 
public class CoinPickup : MonoBehaviour
{
    [SerializeField] private GameManager gameManager;
    [SerializeField] private CoinPickupFeedback feedback;
    [SerializeField] private int scoreValue = 10;
 
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (!other.CompareTag("Player"))
        {
            return;
        }
 
        gameManager.AddScore(scoreValue);
 
        if (feedback != null)
        {
            feedback.Play();
        }
        else
        {
            Destroy(gameObject);
        }
    }
}