- Home /
Flashlight Timer Help
I want to set a time for my flashlight to turn off,can any one help me out please? heres my script for my flashlight and sound.
private var lightSource : Light;
var soundTurnOn : AudioClip;
var soundTurnOff : AudioClip;
function Start () {
lightSource = GetComponent(Light);
}
function Update () {
if (Input.GetKeyDown(KeyCode.F)) ToggleFlashLight();
}
function ToggleFlashLight () {
lightSource.enabled=!lightSource.enabled;
//Audio
if (lightSource.enabled) {
audio.clip = soundTurnOn;
} else {
audio.clip = soundTurnOff;
}
audio.Play();
}
@script RequireComponent(AudioSource)
Also this is in javascript so can you write the timer in javasript also. Thanks :)
Why the double questions? http://answers.unity3d.com/questions/429645/flash-light-timer.html
Answer by EliteMossy · Apr 02, 2013 at 08:55 PM
You are asking someone to make a script for you? I don't see any attempt here to make the flash light length time able.
private var lightSource : Light;
var soundTurnOn : AudioClip;
var soundTurnOff : AudioClip;
var timerStartValue : float = 30;
var timerValue : float = 0;
var timerCoolDownStart : float = 40;
var timerCoolDownValue : float = 0;
function Start () {
lightSource = GetComponent(Light);
timerValue = timerStartValue;
timerCoolDownValue = timerCoolDownStart;
}
function Update () {
if (Input.GetKeyDown(KeyCode.F) && timerValue > 0) {
ToggleFlashLight();
}
if (timerValue > 0 && timerCoolDownValue >= timerCoolDownStart){
timerValue -= Time.deltaTime;
}
else if (timerValue <= 0 && timerCoolDown > 0){
timerCoolDown -= Time.deltaTime;
lightSource.enabled = false;
audio.clip = soundTurnOff;
audio.Play();
}
else{
timerValue = timerStartValue;
timerCoolDownValue = timerCoolDownStart;
}
}
function ToggleFlashLight () {
lightSource.enabled=!lightSource.enabled;
//Audio
if (lightSource.enabled) {
audio.clip = soundTurnOn;
} else {
audio.clip = soundTurnOff;
}
audio.Play();
}
Not tested, nor do i know if it will work.
Your answer
![](https://koobas.hobune.stream/wayback/20220613095552im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
InvokeRepeating work only once 1 Answer
Setting GUI GameObjects to ignore all lighting 1 Answer
check if a timer value is between two numbers 2 Answers
How to make a spawned healthbar not to cast shadow 1 Answer
how to make timer in the game 1 Answer