I have been working on this script trying to make a script, in which every 2 seconds, the battery will decrease, causing the intensity, and range of the torch to drop. However what is happening, is it is waiting 2 seconds, then just ignoring the WaitForSeconds() and decrementing it every frame. How do I go about fixing this, and what exactly have I done wrong?
// Torch light variable.
public Light torchLight;
// Counter for torch battery
float torchBattery = 100.0f;
// Use this for initialization
void Start ()
{
StartCoroutine(batteryTimer());
}
IEnumerator batteryTimer()
{
while (torchBattery > 0.0f)
{
yield return new WaitForSeconds(2);
torchBattery--;
}
do
{
yield return new WaitForSeconds(2);
torchBattery++;
}
while (Input.GetKey("r"));
}