- Home /
Is there a way to break out of a function and return control to another script's function?
I want to make a check before I execute some code in a script because the checks I had beforehand might now (due to some elapsed time) not be true anymore. Is there a way for me, within that if statement check, to break from the script and return control to a function in another script?
Answer by syclamoth · Oct 07, 2011 at 01:38 AM
To manually exit out of a function at any time, use the 'return' keyword. Even for void functions (functions with no explicit return type), it is possible to cancel execution like this!
Along with that, there are the 'break' and 'continue' keywords for controlling execution from within a loop- 'break' automatically skips to the end of the entire loop, and 'continue' skips to the end of that particular iteration.
I'm about to leave for the day, but I'll be looking into this more tomorrow and see how that goes. Another part of my question was if you could return to a specific function ins$$anonymous$$d of just going back to wherever you left off. Thanks for your reply!
void FunctionYouWantToBreakOutOf() { if(someCondition) { SomeOtherFunction(); return; } // whatever else you want to do }
Ah now that's a bit more tricky. I would look at structuring your program such that you don't have to do that. What language are you using? I can probably help you more if you are using C#, since my knowledge of Javascript (Unityscript) doesn't cover some of the more advanced stuff. I'm pretty sure there's something like delegates in Unityscript, but I don't know how to use them.
Unfortunately I'm using JavaScript because there seems to be more examples and documentation for it.
Oh, kevork! I just saw your comment. I'll try that tomorrow as well. Thank you for the idea!
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Breaking out of a function from an if statement inside..... Noob 1 Answer
How to call a function on a prefab script 2 Answers
How to call a function from a script in another scene 5 Answers
Is there a way to override a function in each instance of the same script? 3 Answers