Hiding Object Labels when object is out of Line of Sight

Hello all!

I am having a little bit of an issue. I’m in the process of making an arena FPS and I would like your team’s player name to show up over their head. However, I do not want their name to display when you cannot see them.

I have some objects in my scene that have Object Labels attached to them. I used the code found on [this][1] wiki page to do it. The problem is I can see the name even though I can’t see the objects.

I have this function…

bool LineOfSight (Transform target)
		var fov = 60.0;
		RaycastHit hit;
		 if (Vector3.Angle(target.position - transform.position, transform.forward) <= fov && Physics.Linecast(transform.position, target.position, out hit) && hit.collider.transform == target) 
			return true;
		return false;

It returns true if the object is in LoS. The problem is, the Object Label will flash then the above function will always return false.

Any ideas on how I can accomplish this or what I am doing wrong in my code?

Thank you for the help in advance!``
[1]: http://wiki.unity3d.com/index.php?title=ObjectLabel

You could try switching to renderer.isVisible, renderer.OnBecameInvisible(), and renderer.OnBecameVisible()