Raycasting to turn varible on and OFF

I'm making a FPS game, and i am having some difficulty making a box or chest which is highlighted when the player looks at it.

I am using raycast from the camera to detect if the player is looking at the box. Highlighting the box (Using a different shader) is easy. but how do i detect that the camera is no longer looking at the box, and the switch back to the original shader?

heres my script:

function Update () {
    var fwd = transform.TransformDirection (Vector3.forward);
    var Hit : RaycastHit;
var shader1 = Shader.Find( "Diffuse" );
var shader2 = Shader.Find( "Self-Illumin/Diffuse" );

    if (Physics.Raycast (transform.position, fwd, Hit, 100)) {
        if (Hit.collider.gameObject.tag=="Highlightable")
            Hit.collider.gameObject.renderer.material.shader = shader2;
    }
// and then i am lost...
}

thought that maybe there might be a possibility for the script to be on the box itself and then detect whether or not the ray is pointing at it, but i cant seem to find such a code in the scripting reference.

Thx in advance - Sigvard.

A ray is just a line in space really, the camera views within a frustum, so the ray won't really represent what camera sees, but simply a point in the camera's view. You could cast a series of rays or do something with a trigger or any number of approaches.

Your code above to work as you describe would be something like:

if(Physics.Raycast (transform.position, fwd, Hit, 100) &&
   Hit.collider.tag=="Highlightable")
    Hit.collider.renderer.material.shader = shader2;
else Hit.collider.renderer.material.shader = shader1;

The real problem is that it only captures something that is at the exact center of the screen. A better solution would be to write a better shader which applies a screen-space image effect like the pro image effect shaders. Something where you multiply by the modelviewprojection matrix to get your viewport coordinates which you can use to index a ramp texture to determine how close the coordinate is to the center of the screen.