You could make an Interacter script that you add to your camera that send OnInteract on any ray hit. Then you can easily make a switch for your light(s). Just add a collider to the switch and set up target, range, proper layer masks and you're ready to go. This can be used to do all sorts of interactions.
Interacter.js (Place on Camera)
var interactLayers : LayerMask = -1;
var interactRange : float = 2.0f;
function Update() {
if (Input.GetKeyDown(KeyCode.E)) {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.forward, hit, interactRange, interactLayers))
hit.collider.SendMessage("OnInteract", gameObject, SendMessageOptions.DontRequireReceiver);
}
}
LightSwitch.js (Place on button that has collider)
var target : Light;
function OnInteract() {
target.enabled = !target.enabled;
}
Step by step tutorial:
Create a new Scene and delete the default "Main Camera".
Create a new Cube and call it "Floor". Scale it so it becomes your floor.
Drag in "First Person Controller" prefab into your scene and place it on your floor.
Set layer to "Ignore Raycast" on your "First Person Controller" object (And all children).
Add Interacter script to "First Person Controller/Main Camera". Click Continue.
Set Interacter.interactLayers to every layer except"Ignore Raycast".
Create a new Light and call it "Light". Place it somewhere you can see its light.
Create a new Cube and call it "Button". Place it somewhere you can reach it.
Add LightSwitch to "Button" and set its target to "Light".
Play the game. Walk up to the button and press E. It should now toggle off and on.
Do you want to activate and desactivate the light when the player is around?
Just add a trigger event with a collider, there are many examples in here if you do a search for colliders. Or in the documentation, for examlple here.