Timing while a variable is true

Hey guys,

I’ll try and keep it simple and not confuse with details:

I’m trying to figure out a way to code when the player turns on their candle, it counts in seconds how long they’ve had it on for, then if they’ve had it on for ten seconds, do something. Continue counting until they turn off the candle/light and every ten seconds do that same thing.

If someone out there could help me get started, it doesn’t have to be fancy. That’d be nice. Sorry if it’s been asked a billion times too.

Thanks.

What do you have so far?
Do you have the candle On/Off working?
you can do a simple thing like this.

candleOnTime += Time.deltaTime;
     if ( candleOnTime < 10 )
     {
         //\do code to turn Off candle
         //start a coroutine or what ever
     }

I have the flame turning on and off just fine. It sets PlayLightOnCheck to 1 whenever it’s on. Then I call the FlameDur function which is supposed to be counting how long it’s been on for, and every ten seconds taking 2 points off the FairyHP.

The FairyHP is getting the health points from a separate script with its health points there, that’s working fine.

It’s just not counting up to 10, taking off points, restarting count, getting to 10 remove points… etc.

When I’m turning the candle on, it’s just saying how long it’s been since the game started.

In the FlameDur I just put in what you suggested just now. I’ve had a while loop in there, I’ve had a for loop. I’ve tried recording flamestart, then when the candle is on: flametime = Time.time - flamestart

I’m kinda new to coding, so I’m embarrassed showing my script but here goes :stuck_out_tongue:

private Light flame;
public static int PlayLightOnCheck = 0;
public float flametime = 0;
public float maxtime = 10;
public int FairyHP;
public float flamestart = 0;

void Start ()
{
flame = GetComponent ();

}

public void PlayerLight ()
{

if (flame.enabled == true) {

PlayLightOnCheck = 1;
FlameDur ();

} else {
PlayLightOnCheck = 0;
flametime = 0;
flamestart = 0;
}

}

void Update () {

FairyHP = Fairy.fairyhealth;

if (Input.GetButtonDown (“Candle”))
{
flame.enabled = !flame.enabled;
flamestart = Time.time;
PlayerLight ();
}

}

public void FlameDur () {

flametime += Time.deltaTime;
if (flametime > maxtime)
{
FairyHP -= 2;
}

}

}

you need a Coroutine.
change PlayerLight () with this to start the coroutine,
StartCoroutine(FlameDur());

update the flameDur to this. I free handed the code, sorry for typos.

IEnumerator FlameDur () {
    while (PlayLightOnCheck == 1)
    {
         flametime += Time.deltaTime;
         if(flametime > maxtime)
         {
            FairyHP -= 2;
            flametime = 0; //reset the 10 sec timmer
         }
         yield return null;
    }
}

Also note to please use code tags. Using code tags properly in the future.

One thing I notice is you have a FairyHP = Fairy.fairyhealth; in update, but then in the FlameDur method you are setting FairyHp -=2;

Just note that your update will constantly reset the FairyHP value as it currently sits.

good catch Brathnann, i didn’t see that.

and change flamestart
if (Input.GetButtonDown (“Candle”))
{
flame.enabled = !flame.enabled;
flamestart = 0;
PlayerLight ();
}

Thanks so much guys. All fixed thanks to you. Also glad you told me about that code tag thing, I shall definitely keep it in mind for next time!