Hello,
I am a complet beginner in Unity and scripting and i am trying to do my first coding exercices,
I am stuck on this problem : I have 3 rooms, each of them have collectibles and I have an UI to counter how many collectible remains to take in each rooms.
My problem : I have 3 different script and i would like to combine them so, whenever the player collect the last collectible in a room, the second UI collectiblecounter appears and replaces the first one to proceed in the next room.
Here is two of my scripts (the tird one is similar) :
‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’
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.FindObjectsOfType(collectibleType).Length;
}
// Optionally, check and count objects of type Collectible2D as well if needed
Type collectible2DType = Type.GetType("Collectible2D");
if (collectible2DType != null)
{
totalCollectibles += UnityEngine.Object.FindObjectsOfType(collectible2DType).Length;
}
// Update the collectible count display
collectibleText.text = $"Collectibles remaining: {totalCollectibles}";
}
}
using UnityEngine;
using TMPro;
using System; // Required for Type handling
public class UpdateCollectibleCount2 : MonoBehaviour
{
private TextMeshProUGUI collectibleText; // Reference to the TextMeshProUGUI component
void Start()
{
collectibleText = GetComponent<TextMeshProUGUI>();
if (collectibleText == null)
{
Debug.LogError("UpdateCollectibleCount2 script requires a TextMeshProUGUI component on the same GameObject.");
return;
}
UpdateCollectibleDisplay(); // Initial update on start
}
void Update()
{
UpdateCollectibleDisplay();
}
private void UpdateCollectibleDisplay()
{
int totalCollectibles2 = 0;
// Check and count objects of type Collectible
Type collectibleType = Type.GetType("Collectible2");
if (collectibleType != null)
{
totalCollectibles2 += UnityEngine.Object.FindObjectsOfType(collectibleType).Length;
}
// Optionally, check and count objects of type Collectible as well if needed
Type collectible2DType = Type.GetType("Collectible2D");
if (collectible2DType != null)
{
totalCollectibles2 += UnityEngine.Object.FindObjectsOfType(collectible2DType).Length;
}
// Update the collectible count display
collectibleText.text = $"Collectibles remaining: {totalCollectibles2}";
}
}
Thanks you for your answers