Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question UI ocunter not updating values.

Discussion in 'UGUI & TextMesh Pro' started by Dr_Stavion, Feb 16, 2024.

  1. Dr_Stavion

    Dr_Stavion

    Joined:
    Jan 4, 2024
    Posts:
    1
    using UnityEngine;
    using UnityEngine.UI;
    using TMPro;

    public class CountersDisplayScript : MonoBehaviour
    {
    public TMP_Text EnergyText; // Reference to the UI Text object for displaying energy
    public TMP_Text HealthText; // Reference to the UI Text object for displaying health

    public int health = 3; // Player's health counter
    public int energy = 0; // Portal energy counter
    public int switched = 0; // Flag to track whether the player's color is switched

    // Start is called before the first frame update
    void Start()
    {
    // Initialize UI text objects
    UpdateEnergyText();
    UpdateHealthText();
    }

    // Update is called once per frame
    void Update()
    {
    GetPlayerColor(); // Check for player input to toggle color
    }

    void GetPlayerColor()
    {
    if (Input.GetKeyDown(KeyCode.Space))
    {
    // Toggle the value of switched between 0 and 1
    switched = (switched == 0) ? 1 : 0;
    }
    }

    // Update energy value and UI text
    public void UpdateEnergy(int value)
    {
    // Update energy value
    energy += value;

    // Update energy UI text
    UpdateEnergyText();
    }

    // Update health value and UI text
    public void UpdateHealth(int value)
    {
    // Update health value
    health += value;

    // Update health UI text
    UpdateHealthText();
    }

    // Update energy UI text
    private void UpdateEnergyText()
    {
    EnergyText.text = "Energy: " + energy.ToString();
    }

    // Update health UI text
    private void UpdateHealthText()
    {
    HealthText.text = "Health: " + health.ToString();
    }

    // Collision detection with asteroids
    private void OnCollisionEnter2D(Collision2D collision)
    {
    // Check if the collision occurs with an asteroid prefab
    if (collision.gameObject.CompareTag("Asteroid1") || collision.gameObject.CompareTag("Asteroid2"))
    {
    // Check the switched state to determine the effect on energy and health
    if (switched == 0 && collision.gameObject.CompareTag("Asteroid1"))
    {
    // Update energy when Asteroid1 collides with rocketShip and switched state is 0
    UpdateEnergy(1);
    Debug.Log("Asteroid1 collided with rocketShip and switched state is 0. Energy updated.");
    }
    else if (switched == 1 && collision.gameObject.CompareTag("Asteroid2"))
    {
    // Update energy when Asteroid2 collides with rocketShip and switched state is 1
    UpdateEnergy(1);
    Debug.Log("Asteroid2 collided with rocketShip and switched state is 1. Energy updated.");
    }
    else
    {
    // Decrease health for other cases
    UpdateHealth(-1);
    Debug.Log("Asteroid collided with rocketShip. Health decreased.");
    }
    }
    }
    }
    #This is my code to keep track of score in my game. The player can switch from red to blue with spacebar, to collide with red and blue asteroids.

    The problem I am having is none of these values are being updated.

    My collisions are on a seperate script and are working fine. I even have a Debugger running to check which asteroid we collided with. My asteroids are two prefabs that are generated from a spawner and both of those prefabs and my player have tags. The game is displaying the values on the screen but not updating them.

    I'm at a loss, please help.
     
  2. MCF24

    MCF24

    Joined:
    Mar 10, 2021
    Posts:
    33