Flashlight not working

so im trying to make a toggleable flashlight with F and have the power drain over time but i cant turn the flashlight off with f, heres the script

var LightSource : Light;
static var power : float = 100;
static var turnedOn : boolean = false;
var onoffstate = false;
private var drainSpeed : float = 1.3;

    function Update () {
     if (Input.GetKeyDown("f")) {
     	if (power < 0) {
     		onoffstate = false;
     		GetComponent.<Light>().intensity = 0.0;
    	 }

     		if (GetComponent.<Light>().intensity == 0.35){
     			onoffstate = false;
     			GetComponent.<Light>().intensity = 0.0;
     		}
     		if (GetComponent.<Light>().intensity == 0.0) {
     			GetComponent.<Light>().intensity = 0.35;
     			onoffstate = true;
     		
     	}

     }
     if (onoffstate == true){
     	if (power > 0){
     		power -= drainSpeed*Time.deltaTime;
     	}
     	
     }




 }

i also want it to display some battery textures ive made every 33% charge i dont know how to do that and im unsure why my current script doesnt work, does anyone know how to help?

What? Two same questions but not from the same author???

1 Answer

1

You could very easily do it this way. (this is in C#) but should be very simple to convert.
Please mark as correct if this works.

	void Update(){
		if (Input.GetKeyDown (KeyCode.F)) {
			// if light is OFF - turn it on. else turn it off.
			if (!flashLightObject.activeSelf) {
				flashLightObject.SetActive (true);
			} else {
				flashLightObject.SetActive (false);
			}
		}

		// If the Light is ON drain the battery. (Might want to do it with some sort of timer), other wise might
		// Fly through very fast.
		if(flashLightObject.activeSelf){
			if (power > 0) {
				power -= drainSpeed * Time.deltaTime;
			}
		}

		// Display texture. (Might want to make it where it does run every frame though).
		if (power > 0 && power < 20) {
			// Display Texture (Low Battery)
		} else if (power > 20 && power < 50) {
			//Display Texture (Medium battery)
		}else if(power > 50 && power <= 100){
			// Display Texture. (Full Charge).
		}

	}

Doesn't* run every frame, sorry. Not Does run every frame.

flashLightObject is a GameObject. So whatever object the light is on just drag and drop it into the slot. So much sure it's visible in the Inspector.

For example here's an example tree of the FlashLight setup. Player > FlashLightModel > Light (Make sure Light is a seperate gameObject) and put the script on FlashLightModel. So you can then turn the Light object on and off.

You really need to go back and learn the basics man. If you don't understand how to make a public variable slot, it's like you're just asking someone to write your scripts for you. var flashLightObject: GameObject;

Why would you be using the legacy GUI anyways? I highly recommend using the new UI system. You can then just enable the images/SetActive(true); and/or (false); and go about your way.