- Home /
When pressing F flash does not stay on.
So after I have a flashlight after I make hastorch = true which kinda works but the torch does not stay on. Sorry about the long post. Here is my FlashLight script.
var lightSource : Light;
static var EnergyPerBattery : float = 75;
static var BatteriesLeft : int = 0;
static var turnedOn : boolean = false;
private var drainSpeed : float = 0.5;
static var hastorch = false;
function Update () {
if(hastorch == true){
if (Input.GetKey(KeyCode.F)) ToggleFlashlight();
}
}
function ToggleFlashlight(){
if(hastorch == true){
turnedOn=!turnedOn;
if(turnedOn && EnergyPerBattery>0){
TurnOnAndDrainEnergy();
}
}
else{
lightSource.enabled = false;
}
}
function TurnOnAndDrainEnergy () {
lightSource.enabled = true;
while (turnedOn && EnergyPerBattery>0) {
EnergyPerBattery -= drainSpeed*Time.deltaTime;
yield;
}
lightSource.enabled = false;
}
function OnGUI(){
GUI.Box (Rect (Screen.width - 100,Screen.height - 50,100,50), EnergyPerBattery.ToString());
}
static function AlterEnergy (amount : int){
}
Here is my PickUpTorchScript
var batteryPower : int = 10;
var hit : RaycastHit;
var mesh : GameObject;
var Range : float = 1;
var LookingAt = false;
var Destroyed = false;
function Update (){
var fwd = transform.TransformDirection (Vector3.forward);
var hit : RaycastHit;
if(Physics.Raycast (transform.position, fwd, hit, Range)){
Debug.DrawRay(transform.position, fwd * Range, Color.red);
if( hit.collider.gameObject.tag == "Torch"){
LookingAt = true;
if(Input.GetKeyDown(KeyCode.E)){
Destroy(mesh);
Destroyed = true;
LookingAt = false;
}
else if( hit.collider.gameObject.tag == null){
LookingAt = false;
}
}
}
if(Destroyed == true){
FlashLight.BatteriesLeft += 1;
FlashLight.hastorch = true;
}
}
function OnGUI(){
if(LookingAt == true){
GUILayout.BeginArea (Rect((Screen.width/2)-50, (Screen.height/2) , 100, 100));
GUILayout.Label ("E To Interact");
GUILayout.EndArea ();
}
}
Try replacing Get$$anonymous$$ey with Get$$anonymous$$eyDown
Get$$anonymous$$ey is called every frame that a key is pressed, while Get$$anonymous$$eyDown is only called on the first frame.
Thanks for the answer, but no that does not work sadly.
Answer by dorpeleg · Mar 09, 2013 at 02:48 PM
This is your problem:
function TurnOnAndDrainEnergy () {
lightSource.enabled = true;
while (turnedOn && EnergyPerBattery>0) {
EnergyPerBattery -= drainSpeed*Time.deltaTime;
yield;
}
lightSource.enabled = false;
}
change it to:
function TurnOnAndDrainEnergy () {
while (turnedOn && EnergyPerBattery>0) {
EnergyPerBattery -= drainSpeed*Time.deltaTime;
lightSource.enabled = true;
yield;
}
if (EnergyPerBattery <= 0) {
lightSource.enabled = false;
turnedOn = false;
}
}
Hey thanks
That work but now there is a new problem the Light doesn't switch off when I press F again
Thanks again
Never $$anonymous$$d I got just added
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F)){
lightSource.enabled = false;
}
In the ToggleFlashligh function
Your answer
![](https://koobas.hobune.stream/wayback/20220613094108im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Unload unity player game 0 Answers
Iphone Unity and Best book . 1 Answer
OnTriggerEnter getting delay when object is moving? 1 Answer