- Home /
changing gui texture colors based on health
I want the players GUI texture to change color based on the players health however since you cannot use operators with int variables since something like the example below doesn't work I am out of ideas any help would be appreciated thanks again.
Example of what I tried
var healthTex : Texture2D; var healthMid : Texture2D;
function Update() { if(player health <= 50) { healthTex = health mid; } }
Answer by rowdyp · Feb 22, 2013 at 04:27 PM
Never mind I figured it out. I forgot to access my health variable from my other script thanks anyway ;)
Answer by appearance · Feb 22, 2013 at 04:32 PM
You can have an array of colors and make player's health int as an index in the array then you can assign the color to material's main color.
Ex:
Color[] health_colors = { Color.black, Color.blue, Color.cyan, Color.gray, Color.green, Color.red, Color.magenta, Color.white, Color.yellow, new Color (255,0,0,255) };
int player_health = 50;
int color_index = player_health /10;
renderer.material.color = health_colors [color_index];
Your answer

Follow this Question
Related Questions
Text - Negative Color from Background 0 Answers
How do I make a GUI menu that pops up? 2 Answers
Gui label variable as a string.. 1 Answer