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?
Answer by Cheetoturkey · Oct 25, 2013 at 07:56 PM
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.
}
}
}
}
Oh my god, thank you! I should really learn unity3d code, so I can stop bothering you guys, but thank you, you saved me a lot of trouble!
Answer by Unity_scat · Feb 08, 2016 at 05:23 PM
@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;
}
}
Answer by ItsIcear · Feb 10, 2016 at 05:39 PM
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;
}
}
}
Answer by Hakobik2016 · Jun 17, 2016 at 09:46 PM
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
I made a script to make my flashlight flicker when it's low on battery. But ID$$anonymous$$ how to make it toggleable, but also so that it stops running out of battery when it's off. Here's the script without the toggle.
using UnityEngine; using System.Collections;
public class flashlightScript : $$anonymous$$onoBehaviour {
public float LossInSeconds;
void Start () {
}
//Update Is Classed Once Per Frame
void Update () {
GetComponent<Light>() .intensity -= LossInSeconds * Time.deltaTime;
if(GetComponent<Light>() .intensity <= 1f)
{
float FlickeringDuration = Random.Range(0.001f,0.005f);
StartFlickering (FlickeringDuration);
}
}
void StartFlickering (float Duration)
{
GetComponent<Light>() .enabled = false;
Invoke("EndFlickering",Duration);
}
void EndFlickering ()
{
GetComponent<Light>() .enabled = true;
}
}
Answer by chesterhilly · Jul 24, 2016 at 04:22 PM
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'.
Your answer
Follow this Question
Related Questions
(Newbie) Optimize this android LED-toggle? 0 Answers
Decreasing Light Source 0 Answers
I need help with a Flashlight script 1 Answer
Emissive Material Flicker Effect 0 Answers
UNET, flaslight's range and spot 0 Answers