Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

238
Visualizações
How can i change the color if the value is 25? Because the value "<= 50" is taking over with the yellow color of the "25" value

My "Health <= 50" is changing color if it's under 50. But when i hit 25 i wanna change it to red. (Im in Unity, C#) As tagged. I've tried many compbs and researched but aint found any answers. Thanks for reading!

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SomethingFunction : MonoBehaviour
{
    [SerializeField] Text Informationsystem;
    public int Health 100;

    void Start()
    {
        TxtUI= GetComponent<Text>();
    }

    void Update()
    {
        Test();
    }

    public void Test()
    {
        if(health <= 50)
        {
            TxtUI.color = Color.yellow;
        }
        else if(health <= 25)
        {
            TxtUI.color = Color.red;
        }
        else if(health >= 50)
        {
            TxtUI.color = Color.white;
        }
    }
}```
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Please research how if-else statements work.

The problem here is that if, for example, the health is 19: Then the color will be set to yellow, because 19<50.

Because of the else if statement the other checks are not executed. You can either change the order of if-else statements or remove the else part:

if (health <= 25)
        {
            TxtUI.color = Color.red;
        }
        else if (health <= 50)
        {
            TxtUI.color = Color.yellow;
        }
        else if (health > 50)
        {
            TxtUI.color = Color.white;
        }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda