Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using Unity.VisualScripting;
- using UnityEngine;
- public class Collectable : MonoBehaviour
- {
- private ICollectableBehavior _collectableBehavior;
- private void Awake()
- {
- _collectableBehavior = GetComponent<ICollectableBehavior>();
- if (_collectableBehavior == null)
- {
- Debug.LogError("ICollectableBehavior not found on the GameObject. Make sure a component implementing ICollectableBehavior is added!");
- }
- }
- private void OnTriggerEnter2D(Collider2D collision)
- {
- var player = collision.GetComponent<PlayerMovement>();
- if (player != null && _collectableBehavior != null)
- {
- _collectableBehavior.OnCollected(player.gameObject);
- Destroy(gameObject); // Destroy the collectable once collected
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment