Change light when in trigger and pressing "E" ?

Hey everybody. I’m making a game that need a door to be unlocked using this “electricity box”. When I come into the room the box’s light is red, altough when I enter the trigger, the light turns green and the door unlocks. Everything there is ok.

But the problem is, that I want the light to turn green when I’m in the trigger and pressing E, not by just aproaching it.

Any help would be really nice. Thanks.

Oh and here is the script I got:

function Start(){
}

//Activate the Main function when player is near the box
function OnTriggerEnter(c:Collider)
    {
       if(c.gameObject.tag =="Player") 
 
    {
       light.color = Color.green;
       
       }
}

Right now your code activates any time your player enters the area.

You want to check if a key was pressed, so use Input.getKey()

function OnTriggerEnter(c:Collider)
{
   if(c.gameObject.tag =="Player") 
   {
       if (Input.GetKey ("e"))
       {
       light.color = Color.green;
       }
   }
}

This explains it better

I have also added a boolean switchActivated which you can use to check if true to activate other things :

Updated answer Player toggles light on/off while standing in the trigger zone.

var soundTurnOff : AudioClip;
var soundTurnOn : AudioClip;

var playerInTrigger : boolean = false;
var switchActivated : boolean = false;

function Update()
{
	// check the player is in the trigger zone
	if ( playerInTrigger )
	{
		// check for player input
		if ( Input.GetKeyDown( KeyCode.E ) )
		{
			// behaviour based on the state of switchActivated
			switch ( switchActivated )
			{
				// if switchActivated == false, turn it ON
				case false :
					switchActivated = true;
					
					light.color = Color.green;
					
					audio.clip = soundTurnOn;
					audio.Play();
				break;
				
				// if switchActivated == true, turn it OFF
				case true :
					switchActivated = false;
					
					light.color = Color.red;
					
					audio.clip = soundTurnOff;
					audio.Play();
				break;
			}
		}
	}
}
 
function OnTriggerEnter(c:Collider)
{
	if ( c.gameObject.tag == "Player" )
	{
		playerInTrigger = true;
	}
}
 
function OnTriggerExit(c:Collider)
{
	if ( c.gameObject.tag == "Player" )
	{
		playerInTrigger = false;
	}
}

// ------------------------------------------------------------------------------------

original answer that automatically turned off when player left the trigger.

var soundTurnOff : AudioClip;
var soundTurnOn : AudioClip;

var playerInTrigger : boolean = false;
var switchActivated : boolean = false;

function Update()
{
	if ( playerInTrigger && !switchActivated )
	{
		if ( Input.GetKeyDown( KeyCode.E ) )
		{
			switchActivated = true;
			
			light.color = Color.green;
			
			audio.clip = soundTurnOn;
			audio.Play();//It plays the TurnOn sound
		}
	}
	
	if ( !playerInTrigger && switchActivated )
	{
		switchActivated = false;
		
		light.color = Color.red;
		
		audio.clip = soundTurnOff;
		audio.Play(); //It plays TurnOff sound
	}
}
 
function OnTriggerEnter(c:Collider)
{
	if ( c.gameObject.tag == "Player" )
	{
		playerInTrigger = true;
	}
}
 
function OnTriggerExit(c:Collider)
{
	if ( c.gameObject.tag == "Player" )
	{
		playerInTrigger = false;
	}
}