- Home /
How to hide GUI.box if condition is false?
Hi,
I'm using GUI.box to show a healthbar for the mobs, but I only want to show the GUI.box if there is a target.
This is my code at the moment.
public GameObject target;
public bool existsTarget;
// Use this for initialization
void Start()
{
PlayerAttack pa = (PlayerAttack)GetComponent("PlayerAttack");
target = pa.target;
existsTarget = false;
}
// Update is called once per frame
void Update()
{
if(target != null)
existsTarget = true;
else
existsTarget = false;
}
void OnGUI()
{
if(existsTarget)
GUI.Box(new Rect(500, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
}
Unfortunately this doesn't show any healthbars at all. Any ideas as to why?
Comment
Answer by Gabriel Quijada · Feb 25, 2013 at 01:01 PM
I hide the GUI elements in the screen, but not in visible screen, like this
void OnGUI()
{
if(existsTarget)
{
GUI.Box(new Rect(500, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
}
else
{
GUI.Box(new Rect(Screen.width, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
}
}
this is helpfull because you use the screen parameters like uhm...global? you don't have to know the resolution of screen, Unity calculate this. Good Luck.
Thanks for the reply, however it didn't work unfortunately. The problem is that the GUI.Box doesn't show at all, so at the moment it is already "hidden". Although I did add the else anway, probably solved a problem later on :P
Your answer
![](https://koobas.hobune.stream/wayback/20220613093348im_/https://answers.unity.com/themes/thub/images/avi.jpg)