- Home /
Flashlight turning off, but not back on again?
I am using C# to make a spot light attached to the main camera on the firstpersoncontroller turn off and on again with the same key pressed, but it isn't working. I found out that the Update function goes up until the G key is pressed, when the flashlight turns off, but then stops and doesn't start again, not even registering any further G key presses. Could somebody tell me why it isn't working, and how to fix it?
using UnityEngine;
using System.Collections;
public class Flashlightcontrol : MonoBehaviour {
public Light Flashlight;
private bool LightEnabled = true;
void Update()
{
if(Input.GetKey(KeyCode.G))
{
LightEnabled = !LightEnabled;
Flashlight.active = LightEnabled;
}
}
}
this isnt attached to the light by any chance is it? That would stop it dead cold.
Answer by TheDarkVoid · Aug 27, 2012 at 01:35 AM
The problem is that if the key is help or down for more than one frame it will continue to turn on and off really quickly. To fix this change:
if(Input.GetKey(KeyCode.G))
to this:
if(Input.GetKeyUp(KeyCode.G)) //Triggers for one frame when the key is released.
or this:
if(Input.GetKeyDown(KeyCode.G)) // Triggers for one frame when the key is pressed.
This will resolve your issue unless something else is turning off your light.
Answer by aldonaletto · Aug 27, 2012 at 01:36 AM
Change Flashlight.active to Flashlight.enabled:
Flashlight.enabled = LightEnabled;
active is a GameObject property. If you set Flashlight.active to false, Unity actually does Flashlight.gameObject.active = false, deactivating the game object to which the light belongs, and the scripts attached to it simply stop executing (I bet that the script above is attached to this object...)
EDITED: Also change GetKey to GetKeyDown, as @TheDarkVoid said
Answer by ravsters · Sep 02, 2012 at 03:34 AM
Here is a working script. I'm using it for a game I'm making. It is in Javascript. Let me know if it helps!
var lights : Light;
var OnBrightness = 3.0;
var Flash : float=5.0;
var Angle : float=60.0;
var FlashAngle : float=80;
var FlashDuration : float=0.04;
var SwitchOnSound:AudioClip;
var SwitchOffSound:AudioClip;
private var Off : boolean = false;
private var On : boolean = true;
private static var lightOff = 0.0;
function flashme (FlashEffect){
lights.intensity = Flash;
lights.spotAngle = FlashAngle;
yield WaitForSeconds (FlashDuration);
lights.intensity = FlashEffect;
lights.spotAngle = Angle;
}
function Update()
{
if(Light)
{
if(Off)
{
if(Input.GetKeyDown(KeyCode.F))
{
flashme(OnBrightness);
On = true;
Off = false;
AudioSource.PlayClipAtPoint(SwitchOnSound, transform.position);
}
}
else
{
if(On)
{
if(Input.GetKeyDown(KeyCode.F))
{
flashme(lightOff);
On = false;
Off = true;
AudioSource.PlayClipAtPoint(SwitchOffSound, transform.position);
}
}
}
}
}
Answer by ransomink · Mar 26, 2014 at 08:01 AM
Make sure you do not place this script on the light GameObject; any other will do.
using UnityEngine;
using System.Collections;
public class ScriptFlashlight : MonoBehaviour
{
public Light myLight;// the light to be triggered (on and off)
public bool lightEnabled = true;// if the light is enabled or disabled
// Use this for initialization
void Start ()
{
Debug.Log ( "The light started on!" );
}
// Update is called once per frame
void Update ()
{
// check if the 'F' key was pressed
if ( Input.GetKeyDown ( KeyCode.F ) )
{
lightEnabled = !lightEnabled;// toggle the boolean value (this will initially be false)
myLight.enabled = lightEnabled;// enable or disable the light based on the boolean value
// this is just for testing and debug purposes
// check if the light is enabled
if ( lightEnabled )
{
Debug.Log ( "Light turned on..." );
}
else
{
Debug.Log ( "Light turned off..." );
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613080230im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Flashlight Flicker! 2 Answers
Distribute terrain in zones 3 Answers
When flashlight hits invisible object, make it appear? 0 Answers
Multiple Cars not working 1 Answer