How do i make a Spotlight able to turn on and off in Javascript?

I have been trying all day to get this too work but it just doesn’t seem like it want to.

The script errors i get is usually Unkown Identifier: “myLight”
I could guess this is as simple as that. But i’m new to both programming and unity.

Heres my code in Javascript:

 public var ficklampa : GameObject;
     
    function Start()
    {
       var myLight : Light = ficklampa.GetComponent("Light");
    }
     
    function Update()
    {
       if (Input.GetKeyDown("f"))
       {
          myLight.enabled = !myLight.enabled;
       }
    }

You are very close. Your issue is that you are declaring ‘myLight’ locally in Start(). So once it is out of scope, it no longer exists. As a minor point, don’t use GetComponent() with a string parameter. Pass the type/class. Try this:

public var ficklampa : GameObject;
private var myLight : Light;
 
    function Start()
    {
       myLight = ficklampa.GetComponent(Light);
    }
 
    function Update()
    {
       if (Input.GetKeyDown("f"))
       {
          myLight.enabled = !myLight.enabled;
       }
    }

Note if you put the script on the light game object, you can boil this script down to:

function Update() {
   if (Input.GetKeyDown(KeyCode.F)) {
      light.enabled = !light.enabled;
   }
}

Make a spotlight.
Make an empty GameObject.

make a script like this:

var ficklampa : GameObject;
var onOff : boolean;

function Update ()
{
if(Input.GetKeyDown(KeyCode.F))
{

if(onOff == true)
{
ficklampa.SetActive(true);
onOff = false;
}
else
{
ficklampa.SetActive(false);
onOff = true;
}
}

}

untested script, but shouldn’t be any serious errors.
Just add this script to the empty GameObject, and drag the spotlight into the slot in the inspector for ficklampa