hey I’ve got a C# script that I can’t seem to make a simple logic. I want it to check only once because this variable is always true making it loop again and again. I want it to check only once so that it will work just once even though the variable is always true. so basically what i need is a simple logic script.
here’s a simple script on my dilema to give you an idea on what my problem is
private bool hold;
private bool jumpOnlyOnce;
Update()
{
if(hold == true)//when I Hold, it will return to true every frame
{
if(jumpOnlyOnce == true)
{
jumpOnlyOnce = false;//when its true it will jump
}
else
jumpOnlyOnce = true;
}
}
// i actually got this logic from a Pause Button, the Difference
//is. a Pause Button only gets push once so its not going through
//every frame that's why this wont work since this script is hold
//which is true until its let go. I want it to work only once
//even though hold is always true.
So, if I understand right, you’re asking if there’s a way to automatically assume that the boolean value is “true” after the first time so it doesn’t have to keep checking?
Sorry to tell you this, but that’s COMPLETELY unnecessary. Also, there aren’t really good ways of doing that.
A check of whether a boolean is true or false is one of the fastest tests you can perform in programming. In general, when you’re worried about squeezing out minute fragments of performance, you convert things INTO booleans for a faster comparison. For example, you might compare floats and store the result as a bool to call up more quickly.
But for a once-per-frame test, you’ll need a few billion instances of that script before you would even possibly notice any performance impact from testing whether it’s true or false.
However, what you would more likely notice first is that the Update() functions themselves, let alone billions of instantiated objects and their object and transform data, would be taking a far greater toll on your performance than a single boolean would.
what you want to do is to check for a change in the variable
if (hold == true && lastHold == false){
// do your jump logic
lastHold = hold;
}else if(lastHold == true && hold == false){
lastHold = hold;
}
this will only trigger when hold and lastHold are not the same, then before each part closes lastHold gets the value of hold to keep them the same any time you dont want them to run.