How to get timer to work?
I have implemented a timer into my flashlight script in an attempt to get the flashlight to slowly dim whenever it is on till it runs out of energy completely, at which the player needs to find batteries to replenish the flashlights energy level (not coded yet). However, the energy level only decreases when the flashlight is toggled on, and does not constantly decrease - the energy level just decreases once each time. Can anybody help me?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; //this allows us to reference anything to do with the UI in our script. *MUST BE ADDED IN ORDER TO USE UI IN SCRIPTS*
public class flashlight2 : MonoBehaviour
{
public Light lightSource; //allows us to add a light source to be toggled.
public AudioClip soundOn; //allows us to add a sound to be played everytime the flashlight is toggled.
public AudioClip soundOff;
public KeyCode key; //this will allow us to choose our desired key to toggle the flashlight outside of the script.
public Text isontext; //this is used as a reference to the text that will be updated by our script.
private bool isOn = false; //this will make the text display the light as being off at the beginning of the game, rather than displaying nothing.
public float energy = 100.0f; //this allows us to set the amount of energy that the flashlight has.
public float drainSpeed = 2f; //this is the speed that the energy drains at.
AudioSource lightaudio;
void Awake () //awake is used to initiate game states and variables before the game starts - this is only ran once during the script's lifetime. We only need to grab the audio source once, so we used awake to do so before anything else can happen.
{
lightaudio = GetComponent<AudioSource> (); //grabs the audio source component - allows sound to be played.
}
void Start () //Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.
{
lightSource.enabled = false; //this will start the game with the flashlight off.
displaytext();
}
void Update () //anything within the update function will be called/updated every frame.
{
if (Input.GetKeyDown (key)) //if the specified key (set in the Inspector) is pressed, then do the following
{
lightSource.enabled = !lightSource.enabled; //the "!" operator means not, you can toggle something by setting it to the opposite value of itself, which is what we are doing here with the light source. You can toggle a light source by toggling its enabled state.
isOn = !isOn; //the boolean isOn will be toggled when the specified key is pressed.
displaytext();
playsound ();
if (isOn && energy > 0)
{
drainenergy ();
}
else
{
lightSource.enabled = false;
}
}
}
void displaytext () //using the same line of code more than once in inefficient, therefore I made the line of code a function, so instead of using that same line of code, I can call the function. This uses less memory.
{
isontext.text = "Light: " + isOn.ToString (); //the boolean variable isOn will be displayed on the screen whether it is true or false (testing text).
}
void playsound () //this function allows us to play a different sound when the flashlight is toggled on or off.
{
if (lightSource.enabled == true) //if the flashlight is on, then play the sound indicating that the flashlight has been toggled on.
lightaudio.PlayOneShot (soundOn);
if (lightSource.enabled == false) //if the flashlight is off, then play the sound indicating that the flashlight has been toggled off.
lightaudio.PlayOneShot (soundOff);
}
void drainenergy()
{
lightSource.enabled = true;
while (isOn == true && energy > 0)
{
energy -= Time.deltaTime * drainSpeed;
return;
}
if (isOn == true && energy == 0)
{
lightSource.enabled = false;
}
if (isOn == false && energy > 0)
{
lightSource.enabled = false;
}
}
}
Your answer

Follow this Question
Related Questions
Need help with making disappearing platforms. Error CS1525 1 Answer
My float is always adding 1f to it even with no code telling it to 1 Answer
PowerUp Timer 0 Answers
Unity3d countdown timer is not counting down 0 Answers
Idea to know how to give realistic movements to a spaceship ( A sort of delay ) 0 Answers