Mi "Salud <= 50" cambia de color si está por debajo de 50. Pero cuando llegue a 25 quiero cambiarlo a rojo. (Estoy en Unity, C#) Como etiquetado. Probé muchas combinaciones e investigué, pero no encontré ninguna respuesta. ¡Gracias por leer!
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; } } }```
Investigue cómo funcionan las declaraciones if-else.
El problema aquí es que si, por ejemplo, la salud es 19: Entonces el color se establecerá en amarillo, porque 19<50.
Debido a la declaración else if, las otras comprobaciones no se ejecutan. Puede cambiar el orden de las declaraciones if-else o eliminar la parte else:
if (health <= 25) { TxtUI.color = Color.red; } else if (health <= 50) { TxtUI.color = Color.yellow; } else if (health > 50) { TxtUI.color = Color.white; }