1
0
Files
LupiNexMedia f84f5c592b initial commit
2026-04-28 09:32:03 +02:00

47 lines
1.5 KiB
C#

using UnityEngine;
using TMPro;
using System; // Required for Type handling
public class UpdateCollectibleCount : MonoBehaviour
{
private TextMeshProUGUI collectibleText; // Reference to the TextMeshProUGUI component
void Start()
{
collectibleText = GetComponent<TextMeshProUGUI>();
if (collectibleText == null)
{
Debug.LogError("UpdateCollectibleCount script requires a TextMeshProUGUI component on the same GameObject.");
return;
}
UpdateCollectibleDisplay(); // Initial update on start
}
void Update()
{
UpdateCollectibleDisplay();
}
private void UpdateCollectibleDisplay()
{
int totalCollectibles = 0;
// Check and count objects of type Collectible
Type collectibleType = Type.GetType("Collectible");
if (collectibleType != null)
{
totalCollectibles += UnityEngine.Object.FindObjectsByType(collectibleType, FindObjectsSortMode.None).Length;
}
// Optionally, check and count objects of type Collectible2D as well if needed
Type collectible2DType = Type.GetType("Collectible2D");
if (collectible2DType != null)
{
totalCollectibles += UnityEngine.Object.FindObjectsByType(collectible2DType, FindObjectsSortMode.None).Length;
}
// Update the collectible count display
collectibleText.text = $"Collectibles remaining: {totalCollectibles}";
}
}