Adding time before a script is initiated.

Hey,

So I have a script which makes the intensity of a light to go down over time. You can add wood to make it go back up but I want it to if it reaches intensity 2 to put a timer (yield WaitForSeconds) before the intensity goes back down.

#pragma strict

var addedWood : boolean = false;
var lightStepDown : float = 0.01;
var lightStepUp : float = 1;
var maxIntensity : float = 1;
var waitFor : int;

function Update () 
{
	light.intensity -= lightStepDown * Time.deltaTime;
}

function OnTriggerEnter (Col : Collider) 
{
 	if(Col.gameObject.tag == "Wood" && light.intensity <= maxIntensity)
 	{
 		yield WaitForSeconds(waitFor);
 	 	addedWood = true;
 	 	light.intensity += lightStepUp;
 	 	yield WaitForSeconds(1);
 	 	Destroy(Col.gameObject);
 	 	yield WaitForSeconds(0.5);
 	 	addedWood = false;
 	 	
 	}
 	else if(light.intensity >= maxIntensity)
 	{
 		Debug.Log("Too much wood!");
 		waitFor = waitFor + 10;
 	}
}

The WaitFor var does change to 10 but that doesn’t stop the script from waiting.

So you want the fire to go down constantly but if the player add a lot of wood the it waits for a while before decreasing.

function Update ()
{
   if(!FullFire())
      light.intensity -= lightStepDown * Time.deltaTime;
}

function FullFire()
{
   if(waitFor > 0){
       timer -= Time.deltaTime;
       return true;
   }
   return false;
}

I’m assuming you want to force the player to wait a certain amount of time before adding more wood, and restrict how much wood can be on the fire, yes?

If so, simply track the last time wood was added and when you call this if statement:

if(Col.gameObject.tag == "Wood" && light.intensity <= maxIntensity)

add a check against Time.time to see if it’s been long enough since the last time wood was added to add more.

On a side note, however, you’re checking for equality twice, here, and you make the player wait if -anything- enters the trigger field. I would suggest changing it to this:

#pragma strict
 
var addedWood : boolean = false;
var lightStepDown : float = 0.01;
var lightStepUp : float = 1;
var maxIntensity : float = 1;
var waitFor : int;
 
function Update () 
{
    if(waitFor > Time.deltaTime)
         waitFor -= Time.deltaTime;
    else if(waitFor != 0)// If you want it to add exactly 10 seconds, this is an important step.
         waitFor = 0;
    else
         light.intensity -= lightStepDown * Time.deltaTime;
}

function OnTriggerEnter (Col : Collider) 
{
    if(Col.gameObject.tag == "Wood")
    {
         if(light.intensity + lightStepUp < maxIntensity)
         {
           addedWood = true;
           light.intensity += lightStepUp;
           Destroy(Col.gameObject, 1);
           addedWood = false;
         }
         else
         {
             lightIntensity = maxIntensity;
             Debug.Log("Too much wood!");
             waitFor += 10;
         }
    }
}

[EDIT] Changed everything up to fit how you specified in the comment, better.