I have a GUI that when my players health is 0 then it will show a GUI and it will be red, the script runs fine, no errors, except it isn’t red. Someone please tell me why it isn’t red.
using UnityEngine;
using System.Collections;
public class PlayerCharacteristics : MonoBehaviour
{
public float playerHealth = 100.0f;
void Start()
{
playerHealth = 100.0f;
}
void Update(){
if (playerHealth > 0.0f) {
playerHealth -= 1;
}
}
void OnGUI(){
if(playerHealth == 0){
GUI.backgroundColor = Color.red;
GUI.Box(new Rect(0, 0, Screen.width, Screen.height), "You are dead");
}
}
}
using UnityEngine;
using System.Collections;
public class PlayerCharacteristics : MonoBehaviour
{
private GUIStyle myStyle = null;
public float playerHealth = 100.0f;
void Start()
{
playerHealth = 100.0f;
}
void Update()
{
if (playerHealth > 0.0f)
{
playerHealth -= 1;
}
}
void OnGUI()
{
InitStyles();
GUI.Box(new Rect(0, 0, Screen.width, Screen.height), "You are dead", myStyle);
}
private void InitStyles()
{
if (playerHealth == 0)
{
myStyle = new GUIStyle(GUI.skin.box);
myStyle.normal.background = MakeTex(1, 1, new Color(1f, 0f, 0f, 0.5f));
}
}
private Texture2D MakeTex(int width, int height, Color col)
{
Color[] pix = new Color[width * height];
for (int i = 0; i < pix.Length; ++i)
{
pix *= col;*
}
Texture2D result = new Texture2D(width, height);
result.SetPixels(pix);
result.Apply();
return result;
}
}
I refer [here][1] to write this code, and hope it is what you need.
[1]: http://forum.unity3d.com/threads/change-gui-box-color.174609/