Selecting Object. Changing material via script.

Here I am trying to use Unity for the first time. I Managed to make a simple FPS level and to make an object that, with the Raycast thingy, notices when I’m looking at it from reasonably near. Now I would like for it to be ‘selected’ when I look at it that way, that is, I crated another material with an outline and I would like that the object’s material change when I look at it.
By copying the Raycast code I had on a script on the player and pasting it in a script on the object I created this:

Shader shader1 = Shader.Find ("Toon/Lighted");
	Shader shader2 = Shader.Find ("Toon/Lighted Outline");
			RaycastHit hit;
	Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2, 0));
	renderer.material.shader = shader1;
	if(Physics.Raycast(ray, out hit, 2))
		if(hit.collider.tag == "Computer")
			//Change material
			renderer.material.shader = shader2;
			if( Input.GetButton ("Fire1") ) {
				renderer.material.shader = shader1;

Since the camera is a child of the player it should work, but (even though it doesn’t give me any errors) it doesn’t, the material doesn’t change.
I know that there’s a simpler function that should allow objects to know when the mouse overs over them, but it’s possible to set a distance from the object for that to work? And it even works with FPS?

Thanks for your help.

You don’t want to change the shader, because when you do this Unity is most likely to copy the old parameters to a new shader. Instead add 2 public variables with materials, create them and assign in the inspector and change the material (renderer.material = yourMaterial).