turn off and on a point light

Hi I’m trying to have a point light come on and stay on as a key is pressed and held down…but when that key is released the light should go off…here is the code I’m using…right now it comes on but when i release the key it stays on. …any help?

using UnityEngine;

public class FlashLight : MonoBehaviour
{
    public Light FlashLightObject;
    private bool LightEnabled = false;
   
    void Update ()
    {
        if(Input.GetButtonDown("Flashlight"))
        {
            LightEnabled = !LightEnabled;
            FlashLightObject.enabled = LightEnabled;
        }
    }
}

just a little fix and it should work:

using UnityEngine;

public class FlashLight : MonoBehaviour
{
    public Light FlashLightObject;
    private bool LightEnabled = false;
   
    void Update ()
    {
        if(Input.GetButtonDown("Flashlight"))
        {
            LightEnabled = true;
            FlashLightObject.enabled = LightEnabled;
        }
        if(Input.GetButtonUp("Flashlight"))
        {
            LightEnabled = false;
            FlashLightObject.enabled = LightEnabled;
        }
    }
}
1 Like

hmm…gives me a error:
Assets/NewBehaviourScript.cs(4,14): error CS0101: The namespace global::' already contains a definition for FlashLight’

and it still does the same thing…stays on…

wow …i’m dumb …i had another with the same code…fixed it