| using UnityEngine; |
| using UnityEngine.Events; |
|
|
| namespace UnityAgent |
| { |
| |
| |
| |
| |
| [RequireComponent(typeof(Collider))] |
| public class Pickup : MonoBehaviour |
| { |
| public int value = 10; |
| public AudioClip collectClip; |
| public UnityEvent onCollected; |
|
|
| private void Reset() |
| { |
| GetComponent<Collider>().isTrigger = true; |
| } |
|
|
| private void OnTriggerEnter(Collider other) |
| { |
| if (!other.CompareTag("Player")) return; |
| PickupSystem.Instance?.Collect(value); |
| onCollected?.Invoke(); |
| if (collectClip != null && PickupSystem.Instance != null) |
| PickupSystem.Instance.PlayPickupSound(collectClip); |
| Destroy(gameObject); |
| } |
| } |
|
|
| |
| |
| |
| public class PickupSystem : MonoBehaviour |
| { |
| public static PickupSystem Instance { get; private set; } |
|
|
| public int score { get; private set; } |
| public AudioSource audioSource; |
|
|
| public System.Action<int> onScoreChanged; |
|
|
| private void Awake() |
| { |
| if (Instance != null && Instance != this) { Destroy(gameObject); return; } |
| Instance = this; |
| if (audioSource == null) audioSource = gameObject.AddComponent<AudioSource>(); |
| } |
|
|
| public void Collect(int amount) |
| { |
| score += amount; |
| onScoreChanged?.Invoke(score); |
| } |
|
|
| public void PlayPickupSound(AudioClip clip) |
| { |
| if (audioSource != null && clip != null) audioSource.PlayOneShot(clip); |
| } |
| } |
| } |
|
|