Spotlight on/off

Hi, I want my character to have a flashlight, that they can turn on/off by hitting a key, and I dont know how to do it. I saw the same question, where they said to use this script:

var flashlight : GameObject;

var myLight : Light = flashlight.GetComponent("Light");
function Update()
{

if (Input.GetKey("f"))
{
   myLight.enabled = !myLight.enabled;
}

}

The problem is that its in C#, and I dont know how to write the same code just in JavaScript. Can you help me with the script?

Looks like it's only the declaration that needs updating - note the syntax change.

(Note that the gameObject name is 'flashlight', not lightPoint - as per comments)

public var flashlight : GameObject;

function Start()
{
   var myLight : Light = flashlight.GetComponent("Light");
}

function Update()
{
   if (Input.GetButtonDown("Fire1"))
   {
      myLight.enabled = !myLight.enabled;
   }
}

Let me know if there are any other problems - preferably the compiler error(s)!

var flashlight : GameObject;

var myLight : Light = flashlight.GetComponent("Light");
function Update()
{

if (Input.GetKey("f"))
{
   myLight.enabled = !myLight.enabled;
}

}

I edited this so you can turn off and on the light feel free to use. :slight_smile:

var flashlight : GameObject;
 
  
 var myLight : Light = flashlight.GetComponent("Light"); 
 
 
 function Update() {
 
  
     if (Input.GetKey("f")) { myLight.enabled = !myLight.enabled; 
     }
 
     else if (Input.GetKey("f")) { myLight.enabled = !myLight.enabled == false; 
     }
 
  
 } 

@GMills