JS Flashlight script - stop countdown whilst turned off

Literally at my wits end with this script. ive been working on this for a loooooong time trying to tweak this just the way I want. Im not good with scripting but Im trying hard to understand how everything works and I have a working script but not exactly how I want.

Simply, everything works perfectly but I want the “battery countdown” countdown to stop when I turn the flashlight off. Ive searched online and found conflicting answers between invoke repeating/cancel invoke and time.timescale/Time.time. The scripting reference isnt clear on how timescale works but I dont think Time.time is right for this script…

var flashlightToggle : boolean = false;
static var currentBattPower : int = 100;
static var maxBattPower : int = 100;
var minFlickerSpeed : float = 0.01;
var maxFlickerSpeed : float = 4.0;
var minLightIntensity : float = 0.1;
var maxLightIntensity : float = 1;

function Update () 
{

flashlightOnOff();

countdownBattery();

flickeringFlash();

}

function flashlightOnOff()
{
	if (Input.GetMouseButtonDown(0) && currentBattPower >= 1)
	{
		flashlightToggle = !flashlightToggle;
	}
			if (flashlightToggle == true)
			{
				InvokeRepeating ("countdownBattery", 0 , 1);
				light.intensity = maxLightIntensity;
			}
			if (flashlightToggle == false)
			{
				CancelInvoke("countdownBattery");
				light.intensity = minLightIntensity;
			}
			
}

function countdownBattery()
{
	if (flashlightToggle == true) //turn on
		{
		//This bottom line needs changing so power doesnt continue to count down aftet turning light off.
		currentBattPower = maxBattPower - Time.time; //[current battery power] counting down a second at a time
		Debug.Log("time remaining = " + currentBattPower); //display it on the debug.	
		}
}






function flickeringFlash()
{
	if(currentBattPower <=20 && flashlightToggle == true)
	{
		light.intensity = Random.Range (minLightIntensity, maxLightIntensity);
		yield WaitForSeconds (Random.Range (minFlickerSpeed, maxFlickerSpeed) );
	}
}

Any help would be greatly appreciated as I just need this to work so I can tick this off my to do list. Apologies if this has been asked before - I did search for it but couldn’t find anything concrete. Thanks guys in advance!

Well, you could call CancelInvoke to stop it
(Unity - Scripting API: MonoBehaviour.CancelInvoke)

The problem is that this does not preserve the time elapsed so far. So, to get that, you would have to store the start time and then find the time between the start time and when you cancel, subtract that from your initial time length, and store the result to be used when you started again as the new time duration.

Frankly, this is all MUCH simpler if you don’t use InvokeRepeating at all but just do the repeat in an Update method.

I don’t know about those invokes reminds me of AS1.

I would simply have the mouse down switch a boolean. Then have an if based on that boolean incase all the functions. Rather than this switch switch switch stuff.

So something like this

Replace UPDATE WITH

 function Update () 
 {
 
 flashlightOnOff();
 if (flashlightToggle) {
 countdownBattery();
 
 flickeringFlash();
 }
 }

replace FLASHLIGHTONOFF WITH

    function flashlightOnOff()
     {
         if (Input.GetMouseButtonDown(0))
         {
             if (flashlightToggle && currentBattPower >= 1) {
flashlightToggle=true;
} else {
flashlightToggle=false;
}        
     }

Hello again, I’ve deleted the last answer. Here is a new version of your script that I have made, this works 100%. Do not change anything just put this on an empty object in your scene, press play and press the mouse button to watch it work by watching the vars change in the Inspector.
Once you have seen it work then integrate it into you project properly(and reintroduce you flicker code) and if you get any errors then you know it is to do with one of two things : a) Another Script is interfering with it. b) Your use of static vars is causing problems. This is why I have changed all the variable names slightly, there can be no possible conflict with your static var declarations at all this way.

#pragma strict

//Left all these public so you see them change in the Inspector.
public var bFlashlight_Is_On : boolean = false;
public var fCurrent_Battery_Power : float = 100f;
public var fMax_Battery_Power : float = 100f;

function Update() {

    //I've removed the Flicker Code for now.

    subFlashlight_Switch_On_Off();
    
    if (bFlashlight_Is_On) {
        subReduce_Battery_Power();
    } else {
        subReCharge_Battery_Power();
    }
}

function subFlashlight_Switch_On_Off() {

    if (Input.GetMouseButtonDown(0)) { 
    
        if (bFlashlight_Is_On) {
            bFlashlight_Is_On = false;
    
        } else {
            //Made it have to Recharge to 5 Before re use.
            if (fCurrent_Battery_Power >= 5f) {
                bFlashlight_Is_On = true;
            }
        }
    }

    //Force flashlight off if out of power.
    if (fCurrent_Battery_Power <= 0) {
        bFlashlight_Is_On = false;
        fCurrent_Battery_Power = 0f;
    }
}
 
function subReduce_Battery_Power() {
    
    if ((fCurrent_Battery_Power - Time.deltaTime) > 0) {
        fCurrent_Battery_Power = fCurrent_Battery_Power - Time.deltaTime;
    } else {
        fCurrent_Battery_Power = 0f;
    }
}

function subReCharge_Battery_Power() {

    if ((fCurrent_Battery_Power + Time.deltaTime) < fMax_Battery_Power) {
        fCurrent_Battery_Power = fCurrent_Battery_Power + Time.deltaTime;
    } else {
        fCurrent_Battery_Power = fMax_Battery_Power;
    }
}

I look forward to hopefully hearing back from you that this is working as well for you as it did when I tested it :slight_smile: