- Home /
Stop calling a function
Hey,
In my script I have a function which calls another function when the requirements are met, here:
void FixedUpdate () {
if(rigidbody.velocity.magnitude > TakeOffSpeed)
TakeOff();
if(rigidbody.velocity.magnitude > TakeOffSpeed + 10)
Balance ();
}
What I want to happen is TakeOff() to be stopped when Balance() is called, however TakeOff() is a 'void' function. I have been trying to use return but I get an error, as return cannot be used on a void function!
This is TakeOff():
void TakeOff()
{
rigidbody.AddForce (0,50,0,ForceMode.Acceleration);
rigidbody.AddRelativeTorque (5,0,0,ForceMode.Force);
}
As you can see, I can't use int or float, as it is a force not a value.
I'm not sure if I explained this very clearly, but help would be much appreciated :)
Thanks
Yes you can return from a void. You can't return a value, but you can just do return
(which means exit out)
void myFunc()
{
if (condition)
return;
}
Answer by gardian06 · Sep 13, 2013 at 06:03 PM
assuming this is a non static class. the simplest solution is to create a bool, and have that bool control the functionality of one, or both methods.
so it would look like this
//...
bool controlBool = false;
//...
void TakeOff(){
if(controlBool == false){
// other code
}
}
void Balance(){
controlBool = true;
// other code
}
//...
I had thought about putting a bool in my answer as well. Not sure which he is looking for if either.
Your answer

Follow this Question
Related Questions
Variables are assigned but it returns null 2 Answers
Method returning null, when referenced from another script 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers