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?

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.