Make text disappear when far

I’ve got some hovering text on my NPCs (using this but i got no idea how to make the text disappear when the NPCs are far away or are behind another object

After trying a bunch of things by myself and looking at similar questions on the forum i still cant do it, any help?

This should work :slight_smile:

public float drawDistance = 100;

void OnGUI () {


		float distance = Vector3.Distance(transform.position, Camera.main.transform.position);
		Vector3 direction = Camera.main.transform.position - transform.position;
		direction.Normalize ();

		if(distance < drawDistance && !Physics.Raycast(transform.position, direction)){
			Vector3 posToSreen = Camera.main.WorldToScreenPoint(transform.position);
			GUI.Label(new Rect(posToSreen.x, Screen.height - posToSreen.y, 300, 20), "Mr Sphere");