- Home /
Check if a function is no longer being called?
So I have a function being called and I was wondering if there is a way to check if it's no longer being called so I can set a bool to false? Here is a part of my script:
void Update (){
startPos = new Vector3(transform.position.x, startHeight, transform.position.z);
if(goUp){
transform.position = Vector3.MoveTowards (transform.position, (startPos + new Vector3(0, 5, 0)), Time.deltaTime*speed);
}
else
transform.position = Vector3.MoveTowards (transform.position, startPos, Time.deltaTime*speed);
}
public void OnPressUp () {
//goUp=!goUp;
goUp = true;
}
I want to check if OnPressUp is no longer being called so I can set the bool goUp to false.
Answer by Baste · Mar 11, 2015 at 10:50 PM
A function that's not async or a coroutine will always be finished unless the program flow is actually in the function. When you do something like this:
int x = 5;
DoAThing();
int y = 10;
Everything in DoAThing will be executed before the int y = 10 line is reached. So it doesn't make any sense to check if your "OnPressUp" function isn't being called any more, as it's not being called anywhere outside itself.
What your asking doesn't really make sense. What does "no longer being called" mean?
Not called last update? Functions don't have "recently used" timestamps, so you have to make one. Won't be called during the next update? How did your program decide that?
It's like asking how to check you aren't having meatloaf for dinner. There's no built-in way. But, if your have a meal list on the Fridge, check what it says for tonight.
It often helps to forget about functions, blah, and thing about what you want to happen in the game.
Answer by Cherno · Mar 11, 2015 at 11:18 PM
You can set a bool to true in the function, and then in LateUpdate() set it to false, and in Update() you can check if the bool is true or false.
Answer by DiegoSLTS · Mar 12, 2015 at 12:14 AM
I think I understand what you want.
From the code you shared I guess you are detecting when the "Up" key is pressed and you set that goUp variable to "true". You keep the button pressed and your object keeps moving up, but when you release the Up key, it keeps going up. So, you want a way to detect when the up key is released, am I right?
If you managed to detect a key being pressed you should be able to do something similar to detect when a key is released. For example, if you used the Input.GetKeyDown method to check if a key was pressed, you can use Input.GetKeyUp to check if it was released. It could be done in different ways, if you share the actual code that detects the key being pressed it would be easier to help you with the release event.
When you detect a release event you have to call a different function that sets goUp to false.
Just to be clear, when you hold a the Up key down the function is not "being called" during the whole time you held it down, it's called one time that last a few milliseconds. Functions are not "being called", functions are called and they finish, and can be called again and again.
Your answer
Follow this Question
Related Questions
call function with same name + number 1 Answer
How to make on/off-like gui-button? 3 Answers
How can I write: if this method (X) is activated by another specific method (Y) do this (z) 1 Answer
How can I stop executing a Function in a void Function? 1 Answer
What is the best way to call a function from another script 1 Answer