- Home /
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.
Answer by fafase · Aug 25, 2013 at 06:28 PM
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;
}
Note, there may be other way, it is just this one seems simple to grasp.
(30,28): UCE0001: ';' expected. Insert a semicolon at the end. (30,18): BCE0044: expecting (, found ':'.
That's is the errors I got but I'm not sure if I'm doing this right. I added the function update and then just make the function. Is that all I do?
Yes it is my function declaration I guess. Just remove the :Boolean. I never remember how this one works in UnityScript but it is not needed so you can take it off:
function FullFire()
{
if(waitFor > 0){
waitFor -= Time.deltaTime;
return true;
}
return false;
}
Answer by DESTRUKTORR · Aug 25, 2013 at 06:38 PM
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.
I was thinking that if the max intensity is 2 and after adding the required amount of wood to match the max, then a 10sec wait would happened before the intensity started to drop again. Like fafase said.
Ah. That would require a bit of a change in your logic, then. I'll apply those edits to my answer :P
It works..kind of. It reaches 2 and says Too $$anonymous$$uch wood and I can see for a spit seconds a number go down but it's not in seconds.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Flashlight timer 2 Answers
Directional light - Help 0 Answers
Help make this timer work? 2 Answers
Cannot Get Light To Flash - Help 1 Answer