Flashlight toggle

Now, I did have this script at one time, but I lost it. It was a really good script that allowed you to toggle your flashlight with one click of a button, complete with sounds! But as I said, I lost it. And I have been wandering around this forum thingy looking for the right one, but they don’t toggle, they just flicker the flashlight on/off really fast, and they have no sound. And I’ve tried to learn java before, but my little [AGE WITHHELD] brain couldn’t handle it, and trust me, I would love to learn javascript, but it’s just a little bit too complicated. Wow, now I have two questions. Actually, I can use google for the second question… But does anyone know a script that does what I described?

var flashlightOn : boolean = false;

function Update () {

    //Checks if the boolean is true or false.

    if(flashlightOn == true){

        light.intensity = 1;//If the boolean is true, then it sets the intensity to what ever you want.

    } else {

        if(flashlightOn == false){

            light.intensity = 0;//If the boolean is false, then it sets the intensity to zero.

        }

    }

    

    //Checks if the F key is down and whether the boolean is on or off.

    if(Input.GetKeyDown(KeyCode.F) && flashlightOn == false){

        flashlightOn = true; //If the f key is down and the boolean is false, it sets the boolean to true.

    } else {

        if(Input.GetKeyDown(KeyCode.F) && flashlightOn == true) {

        flashlightOn = false;//If the f key is down and the boolean is true, it sets the boolean to false.

        }

    }

    

}

}

@Cheetoturkey
I have a better solution. This code turns on/off the object’s Light component. Replace “Flash” with the button that associates with turning on and off. You can edit inputs. Edit → Project Settings → Input

function Update () {
 if (Input.GetButtonDown("Flash")) {
  if (GetComponent.<Light>().enabled == true)
  GetComponent.<Light>().enabled = false;
  
  else
  
  GetComponent.<Light>().enabled = true;
 }
}

My solution (In C#)

// Variables
public bool active = true;
public GameObject flashlight;

// Update Function
void Update()
{
   // If The Flashlight Button Is Pressed
   if(Input.GetButtonDown("Flashlight"))
   {
        // Toggles The Active Bool
        active = !active;

        // If It Is Active
        if(active)
        {
              flashlight.GetComponent<Light>().enabled = true;
        }

        // If Not
        else
        {
             flashlight.GetComponent<Light>().enabled = false;
        }
    }
}

Guys. IDK how to find a page so I can ask my own question. SO i went here since it’s sorta related to my question

Here is a simple way to turn a light on and off by Clicking

 using UnityEngine;
 using System.Collections;
 
 public class Flashlight : MonoBehaviour {
 
     public Light light;    //assign gameobject with light component attached
 
     void Update () {
         if (Input.GetMouseButtonDown (0)) {      //Left mouse button
             light.enabled = !light.enabled;      //changes light on/off
         }
     }
 }
  • Make sure you add the script to GameObject with the light component.

  • Then assign the same GameObject to ‘light’.