- Home /
How to call a function only once in Update
Hi guys, the problem I'm having within my script is that I need to be able to detect when a particle emitter should activate and when it does remove a certain amount of points from the score.
Now, it does activate the emitter and also DOES remove points from the score, the problem is that due to it having to be within the "function Update" it's removing points every frame which results in the score being drastically reduced.
What I want to know is if there's anyway I can make it so that it will only be called the one time or if there's a possible workaround for the problem.
Code is below:
var Beep : AudioClip;
public var wheelFL : WheelCollider;
public var fire : ParticleEmitter;
public var explosion : ParticleEmitter;
private var lastMovementTime : float = Mathf.Infinity;
function Start()
{
fire.emit = false;
lastMovementTime = Time.time;
}
function Update ()
{
if (wheelFL.rpm > 0.2)
{
lastMovementTime = Time.time;
}
else
{
if ((Time.time - lastMovementTime) > 5)
{
audio.PlayOneShot(Beep);
//flash_headlights();
}
if ((Time.time - lastMovementTime) > 9)
{
onFire();
}
}
if ((fire.emit == true) && (Time.time - lastMovementTime > 19))
{
explode();
}
}
function onFire()
{
fire.emit = true;
StatisticsScript.scorePoints -= 5;
}
function explode()
{
explosion.emit = true;
yield WaitForSeconds (3.0);
Destroy (gameObject);
StatisticsScript.scorePoints -= 10;
}
Thanks guys, any help will be greatly appreciated.
if ((fire.emit == true) && (Time.time - last$$anonymous$$ovementTime > 19))
{
last$$anonymous$$ovementTime = Time.time;
explode();
}
Answer by stevethorne · Dec 05, 2013 at 07:48 PM
You probably don't want to have the fire.emit as true when it explodes, so you could turn that off in the explode function. This will stop explode from being called multiple times. Your explode function would look like this:
function explode()
{
fire.emit = false;
explosion.emit = true;
yield WaitForSeconds( 3.0 );
Destroy( gameObject );
StatisticsScript.scorePoints -= 10;
}
Alternatively you could add another bool in your code that keeps track of whether or not your object has exploded. Then your if statement could read like so:
if ( ( fire.emit == true ) && ( Time.time - lastMovementTime > 19 ) && !exploded )
In your explode function you can set exploded to true. And wherever you spawn the object you can set exploded to false.
Although useful, this doesn't actually answer the question I've posed as it would still be removing points every frame while the fire particle emitter is still active.
Then you could use a boolean to keep track of whether or not you have removed the points, then you can set it to true when it removes the points, and set it back to false whenever you want to be able to remove points again.
So your code would have this if statement now:
if ( !hasRemovedPoints )
{
StatisticsScript.scorePoints -= 10;
hasRemovedPoints = true;
}
Your answer
Follow this Question
Related Questions
Fixed timestep... 1 Answer
Is It Bad To Have Your Jump In Update()? 3 Answers
Execute code every x seconds with Update() 4 Answers
Can you redefine functions with names like Update(), etc.? 2 Answers
Update increment error (2 + 1 = 0?) 1 Answer