Object Name Displays fom Everywhere

This script is working perfectly, it display a frase on objects each time the player poses the cursor over the them(a cube with this script can become a sign that tells “Exit Door” each time we put the cursor over it. The main problem is it doesn’t matter if I am 100 feet away from the object, if the cursor passes over the object, the frase will be displayed. I need some help because I can’t find the way to make an extra variable that says "This will happen, only “OnTriggerEnter”, so the message get’s displayed when you are near this object. I’m not good at scripting, and i will really apreciate some help. ANy suggestions:)

This is the script:

var toolTipText = “”; // set this in the Inspector

private var currentToolTipText = “”;
private var guiStyleFore : GUIStyle;
private var guiStyleBack : GUIStyle;

function Start()
{
guiStyleFore = new GUIStyle();
guiStyleFore.normal.textColor = Color.white;
guiStyleFore.alignment = TextAnchor.UpperCenter ;
guiStyleFore.wordWrap = true;
guiStyleBack = new GUIStyle();
guiStyleBack.normal.textColor = Color.black;
guiStyleBack.alignment = TextAnchor.UpperCenter ;
guiStyleBack.wordWrap = true;
}

function OnMouseEnter ()
{

currentToolTipText = toolTipText;

}

function OnMouseExit ()
{
currentToolTipText = “”;
}

function OnGUI()
{
if (currentToolTipText != “”)
{
var x = Event.current.mousePosition.x;
var y = Event.current.mousePosition.y;
GUI.Label (Rect (x-149,y+40,300,60), currentToolTipText, guiStyleBack);
GUI.Label (Rect (x-150,y+40,300,60), currentToolTipText, guiStyleFore);
}
}

All of the OnMouse functions in Unity raycast to an infinite distance. So it doesn’t matter how far away something is when the mouse function is triggered. You might be able to limit the distance by calling for a second raycast like this:

function OnMouseOver()
{
    // Create a ray from the main camera that passes
    // through the mouse pointer
    var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    var hit: RaycastHit;

    // do a Raycast up to 50 meters from the starting point
    if (Physics.Raycast(ray, hit, 50))
       currentToolTipText = toolTipText;
    else
       currentToolTipText = "";
}

function OnMouseExit()
{
    currentToolTip = "";
}

EDIT: Code Updated to Include OnMouseExit() Call.

Everything should now work with the above snippet. When you move away from the object it will clear the text and the text will clear when the mouse is no longer over the object.

Haha yes I realized about the problem on the OnMouseExit, your idea worked in some way, I think the last thing to make it work properly is to put the OnMouseExit somewhere on the script and it should work. By now with the following lines it works in every way except when you are going away from the object while pointing at it(in that case, the text doesn’t dissapear):

function OnMouseOver()
{

var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;


if (Physics.Raycast(ray, hit, 5))
   currentToolTipText = toolTipText;

}

function OnMouseExit ()

{

currentToolTipText = "";

}