Remove GUI's
i have a GUI which shows characters health i want to remove this GUI after my Character died how can i do this this is my scripts;
#pragma strict
var healthBar: PlayerStatsV2;
function OnGUI()
{
if (healthBar.damaged==true)
{
GUI.backgroundColor = Color.red;
}
else
{
GUI.backgroundColor = Color.green;
}
GUI.Button(Rect(20, 20, healthBar.Health, 20),"health");
}
Answer by terrawah · Jan 18, 2016 at 05:44 PM
You could do something like this is your update function:
function Update()
{
if (healthBar.Health <= 0)
{
Destroy(this);
}
}
Which would check every frame if your health is equal to or less then zero. If it turns out that is less then or equal to zero, then it removes this script from the gameobject.
Alternatively, taking your current script, you could add:
function OnGUI()
{
if (healthBar.damaged==true)
{
GUI.backgroundColor = Color.red;
}
else
{
GUI.backgroundColor = Color.green;
}
if(healtBar.Health < 0){
GUI.Button(Rect(20, 20, healthBar.Health, 20),"health");
}
}
Which instead checks to see if your health is greater then zero - and only displays the health bar GUI if it is.
There are a number of other ways to do this as well - like most things with scripting. But I hope this points you in the right direction at least.
Answer by lassade · Jan 17, 2016 at 05:24 AM
Below line 4 put this:
if (healthBar.Health <= 0 ) return;
Your answer
![](https://koobas.hobune.stream/wayback/20220612060504im_/https://answers.unity.com/themes/thub/images/avi.jpg)