- Home /
Calling a function in Update once every time when the condition is met?
I have a game in which one random item is spawned when a timer reaches zero. This is currently in the Update function as it needs to check the timer every frame. But when timer reaches zero, it spawns multiple items because it is in the Update function. I have tried:
if(!singleExecution)
{
if(this condition is met)
{
execute this action;
}
singleExecution = true;
}
this allow the function to be executed once in the Update function when the condition is met the first time. However it does not get executed once every time the condition is met, which exactly is what I need. So Update function doesn't work, what does?
Answer by allenallenallen · Dec 23, 2015 at 09:57 AM
You have an error there. What if the condition isn't met but the timer still reached zero? Then the inner if statement will be discarded BUT the boolean singleExecution will still set to be true.
This is how it should be:
if (!singleExecution)
{
if(this condition is met)
{
singleExecution = true; // This boolean should be inside the if statement
execute this action;
}
}
the condition is the timer. when the timer reaches zero, the condition is met.
Answer by wibble82 · Dec 23, 2015 at 10:25 AM
@allenallenallen pointed out the first issue with your logic, however I think you are also saying that you want your code to:
'execute this action' once when the condition is first met
wait until it is no longer met
'execute this action' when it is met again
goto 2
To do this, you would take a similar fix to the one proposed by @allenallenallen :
//check if the condition is currently met
if(this condition is met)
{
//it is met, so check if we have executed yet
if(!singleExecution)
{
//we hadn't executed yet, so set the single execution bool and execute
singleExecution = true;
execute this action;
}
}
else
{
//it isn't met, so clear out 'singleExecution' bool
singleExecution = false;
}
-Chris
This doesn't really work for me. The action gets executed once when I run the game, and when the condition is met the second time, the action does not get executed because singleExecution is set to true after the first execution.
Well... it kind of hard to explain so I will post the whole thing.
//If progress = 100, Spawn items.
if(progress == 100)
{
CancelInvoke("consumeFood");
CancelInvoke("increaseProgress");
canRest = true;
canHunt = true;
if(level >= 1)
{
if(!singleExecution)
{
SpawnFur();
singleExecution = true;
}
}
else
{
singleExecution = false;
}
}
the "Level >= 1" condition is always met, as the player's lowest level is 1. So the ElSE statement that clear out the singleExecution will never be executed. How can I make this script, so that when the progress reaches 100, spawns 1 item, while checks the progress every frame?
Sorry if I wasn't clear with my question.
Your answer

Follow this Question
Related Questions
Setting Quaternion , are Euler angles updated in the same update function? 2 Answers
Endless runner, road segments spawn speed 0 Answers
my object instantiates to much 1 Answer
BCE0023 Error 1 Answer
Unity increasing more than i want! 1 Answer