johnnygoodguy2000

Collectable.cs

Dec 25th, 2024
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.89 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.VisualScripting;
  4. using UnityEngine;
  5.  
  6. public class Collectable : MonoBehaviour
  7. {
  8.     private ICollectableBehavior _collectableBehavior;
  9.  
  10.     private void Awake()
  11.     {
  12.         _collectableBehavior = GetComponent<ICollectableBehavior>();
  13.  
  14.         if (_collectableBehavior == null)
  15.         {
  16.             Debug.LogError("ICollectableBehavior not found on the GameObject. Make sure a component implementing ICollectableBehavior is added!");
  17.         }
  18.     }
  19.  
  20.     private void OnTriggerEnter2D(Collider2D collision)
  21.     {
  22.         var player = collision.GetComponent<PlayerMovement>();
  23.         if (player != null && _collectableBehavior != null)
  24.         {
  25.             _collectableBehavior.OnCollected(player.gameObject);
  26.             Destroy(gameObject);  // Destroy the collectable once collected
  27.         }
  28.     }
  29. }
  30.  
Tags: collectable
Advertisement
Add Comment
Please, Sign In to add comment