Need help with a lighting script...

So, in my game there’s a lighter in your hand. I have changed a flashlight script to achieve what I wanted… But what I want to add now is a system so that if you have the lighter on for more than… Lets say 60 secs, the lighter will go out. If someone could maybe show me how that’s added into a script it would be much appreciated. Here’s my current script. Thanks.

var Flashlight : Light;

var Flame : ParticleSystem;
 
var Volume : float = 0.5;
 
var LightOn : boolean = true;
 
function Start () {
audio.volume = Volume;
}
 
function Update () {
 
if(Input.GetKeyUp(KeyCode.F))
{
       
        
       
        if(LightOn)
        {
                LightOn = false;
        }
        else
        {
                audio.Play();
                LightOn = true;                
        }
       
        Lighten();
       
}
 
}
 
function Lighten(){
 
        if(LightOn)
        {
                yield WaitForSeconds(0.5);
                Flashlight.light.active = true;
                Flame.particleSystem.active = true;
               
        }
        else
        {
                Flashlight.light.active = false;
                Flame.particleSystem.active = false;
        }
       
}
 
 
 
@script RequireComponent(AudioSource)
@script RequireComponent(ParticleSystem)

Inside your update function, you would have a timer (starting from 60 or whatever value you prefer) counting down. So when it has reached to 0 or less, you have exceeded 60 seconds then set your boolean LightOn to be false. The rest of your script will take care of itself.

if(LightOn) // if your light is turned on
{
	if(timer <= 0)
	{
		LightOn = false;
	}
	else
	{
		timer -= Time.deltaTime;
	}
}

Of course, it’s a good idea to reset your timer to 60 when your turn your light off.

I added in this into the script, under the function update. I got an error for the timer (unknown identifier) so then I thought maybe it needs a variable, added one in as such… “var timer : int = 10;” (10 just for testing) It got rid of the error… but the timer is going down almost immediately and the light isn’t going, also the input isn’t working. I’m thinking It’s either a wrong variable, or Iv’e put something in the wrong place? I’ll post the script as it is below. Thanks again.

I am not very good at JS (use C#) but i came up with this, not tested so unsure, but it should give you an idea to make your code better

var flashLight : Light;
var flame : ParticleSystem;
var volume : float = 0.5;
var isLightOn : boolean = true;
var timerDuration : float = 60;
var timerRunTime : float = 60;

function Start () {
	audio.volume = volume;
}

function Update() {
	if (Input.GetKeyUp(KeyCode.F)){
		if (isLightOn) {
			isLightOn = false;
			flashLight.light.active = false;
            flame.particleSystem.active = false;
            timerDuration = timerRunTime; //Reset timer here
		}else{
			audio.Play();
			isLightOn = true;
			TurnLightOn();
		}
	}
	
	if (timerDuration <= 0){ //Turn of flash light and reset timer when we hit 0!
		isLightOn = false;
		flashLight.light.active = false;
        flame.particleSystem.active = false;
		timerDuration = timerRunTime; //Reset timer here
	}
	
	if (isLightOn  timerDuration > 0){ //Reduce only if light is on and timerDuration > 0
		timerDuration -= Time.deltaTime; //Reduce timer here
    }
}

function TurnLightOn(){
    yield WaitForSeconds(0.5);
    flashLight.light.active = true;
    flame.particleSystem.active = true;
}

@script RequireComponent(AudioSource)
@script RequireComponent(ParticleSystem)

Thank you so much! It works perfectly. Is C# better/easier? I feel Iv’e learnt so much in java, will it be like starting over?

Not really, there is a few differences but not nothing major