Here's a script called TestEventUser that demonstrates how to subscribe to and use the OnItemPickedUp event from your ItemPickup class:
using UnityEngine;
public class TestEventUser : MonoBehaviour
{
[SerializeField] private int totalItemsCollected = 0; // Track total items collected
[SerializeField] private AudioClip pickupSound; // Optional sound to play on pickup
private AudioSource audioSource; // Reference to AudioSource component
private void Awake()
{
// Get or add AudioSource component
audioSource = GetComponent<AudioSource>();
if (audioSource == null && pickupSound != null)
{
audioSource = gameObject.AddComponent<AudioSource>();
}
}
private void OnEnable()
{
// Subscribe to the event when this object is enabled
ItemPickup.OnItemPickedUp += HandleItemPickup;
}
private void OnDisable()
{
// Unsubscribe from the event when this object is disabled
ItemPickup.OnItemPickedUp -= HandleItemPickup;
}
private void HandleItemPickup(ItemPickup item)
{
// Add the item's value to our total
totalItemsCollected += item.GetItemValue();
// Play pickup sound if we have one
if (pickupSound != null && audioSource != null)
{
audioSource.PlayOneShot(pickupSound);
}
// Log the pickup information
Debug.Log($"Item picked up! Value: {item.GetItemValue()} | Total Collected: {totalItemsCollected}");
}
// Optional: Public method to get the total items collected
public int GetTotalItemsCollected()
{
return totalItemsCollected;
}
}
How to Use This Script:
-
Create a GameObject: Add this script to a GameObject in your scene (e.g., your Player or a GameManager).
-
Configure in Inspector:
- If you want sound effects, assign an
AudioClip to the pickupSound field in the Inspector.
- The
totalItemsCollected field will show you how many item points have been collected (you can make this private if you don't want it visible in the Inspector).
-
Event Subscription:
- The script automatically subscribes to the
ItemPickup.OnItemPickedUp event in OnEnable() and unsubscribes in OnDisable(). This is good practice to prevent memory leaks and unexpected behavior.
-
What Happens on Pickup:
- When an item is picked up, the
HandleItemPickup method is called.
- It adds the item's value to the
totalItemsCollected counter.
- It plays a sound effect (if configured).
- It logs information to the Unity Console.
Example Use Cases:
- Score System: You could use this script as part of a scoring system, where
totalItemsCollected represents the player's score.
- UI Updates: You could extend this script to update a UI element (like a score text) by adding a reference to a UI Text component.
- Game Progression: You could use the total items collected to trigger game events (e.g., unlocking a door when the player collects 10 items).
Example Extension for UI:
If you want to update a UI Text element with the total items collected, you could modify the script like this:
using UnityEngine;
using UnityEngine.UI; // Required for UI elements
public class TestEventUser : MonoBehaviour
{
[SerializeField] private int totalItemsCollected = 0;
[SerializeField] private AudioClip pickupSound;
[SerializeField] private Text scoreText; // Reference to UI Text element
private AudioSource audioSource;
private void Awake()
{
audioSource = GetComponent<AudioSource>();
if (audioSource == null && pickupSound != null)
{
audioSource = gameObject.AddComponent<AudioSource>();
}
UpdateScoreUI(); // Initialize UI
}
private void OnEnable()
{
ItemPickup.OnItemPickedUp += HandleItemPickup;
}
private void OnDisable()
{
ItemPickup.OnItemPickedUp -= HandleItemPickup;
}
private void HandleItemPickup(ItemPickup item)
{
totalItemsCollected += item.GetItemValue();
if (pickupSound != null && audioSource != null)
{
audioSource.PlayOneShot(pickupSound);
}
UpdateScoreUI();
Debug.Log($"Item picked up! Value: {item.GetItemValue()} | Total Collected: {totalItemsCollected}");
}
private void UpdateScoreUI()
{
if (scoreText != null)
{
scoreText.text = $"Score: {totalItemsCollected}";
}
}
public int GetTotalItemsCollected()
{
return totalItemsCollected;
}
}
Notes:
- Event Safety: Always remember to unsubscribe from events in
OnDisable() to prevent errors if the object is destroyed or disabled.
- Testing: To test this, ensure you have items in your scene with the
ItemPickup script attached, and that your player has a collider tagged as "Player".
- Customization: You can modify the
HandleItemPickup method to do whatever you want when an item is picked up (e.g., trigger animations, spawn effects, etc.).
Let me know if you need further clarification or additional features!