using UnityEngine;
using System.Collections;
public class FlashLight : TouchLogic
{
static float energy = 1000f; //The energy amount of the flashlight
private float drainSpeed = 2.0f; //The speed that the energy is drained
public Light lightSource;
static bool turnedOn = false;
public bool disableTouches;
void Update()
{
//you can do some logic before you check for the touches on screen
if(!disableTouches)//(optional) dynamically change whether or not to check for touches
CheckTouches();
}
public override void OnTouchBegan()
{
ToggleFlashlight();
}
void ToggleFlashlight()
{
turnedOn=!turnedOn;
if (turnedOn && energy > 0)
{
TurnOnAndDrainEnergy();
}
else
{
lightSource.enabled = false;
}
}
IEnumerator TurnOnAndDrainEnergy()
{
lightSource.enabled = true;
while (turnedOn && energy>0) {
energy -= drainSpeed*Time.deltaTime;
yield return energy;
}
lightSource.enabled = false;
}
static void AlterEnergy (int amount) {
energy = Mathf.Clamp(energy+amount, 0, 1000);
}
}
I think the problem is the TurnOnAndDrainEnergy(), i don't know why but, if i erase the while, and let just the enabled = true, the flashlight works, but i need the while to decrease the battery.
↧