- Home /
UI color issue
Hi, I am trying to change the color of the timer to red(warningColor) once it reaches below a certain time. But when i play the game and the countDown reaches 10 or below all the text elements(timer, level name, score) turn red(warningColor) and stay red even when i exit play mode, and reset back to white when i enter play mode again. briefly, how do i change timer's color without affecting the other? public class GameManager : MonoBehaviour { #region VARIABLES public GameObject player; public GameObject obstacle; public Text timer; public Text score; public Text levelName; public float countDown; public Color warningColor; public Color defaultColor; #endregion
void Start()
{
LevelName();
}
void Update()
{
DisplayScore();
Timer();
Fall();
}
void DisplayScore()
{
score.text = player.transform.position.z.ToString("0");
}
void LevelName()
{
levelName.text = "Level: " + SceneManager.GetActiveScene().buildIndex + 1.ToString();
}
void Timer()
{
countDown -= 1 * Time.deltaTime;
timer.text = "Time Left: " + countDown.ToString("0");
if (countDown <= 0)
{
countDown = 0;
Debug.Log("Time's Up");
}
if(countDown <= 10)
{
timer.material.color = warningColor;
}
else
{
timer.material.color = defaultColor;
}
}
void Fall()
{
if(player.transform.position.y < -5)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
}
Answer by TreyH · Feb 15, 2018 at 05:08 PM
You're changing the shared material instead of the color of that element. Use
timer.color = warningColor;
When a material's color changes, anything using that material will also change their appearance. These changes will persist through scene changes.