- Home /
Looping t wo functions until a condition is not met?
Hello. I want to go through a function and if a condition is met, call another function. After it does it's actions, the second function will always call the first function. Unity says I cannot do this because it creates a loop...but it's not technicaly a loop, right (it will stop when the condition is not met)? How can I do this:
function MyFunction1(){
do some stuff
yield;
if(condition==true)
MyFunction2()
}
function MyFunction2()
do some more stuff
yield;
MyFunction1();
}
are you sure you actually want or need to use yield in there? why?
in general if you need to call a coroutine in a situation where you "can't", youjust use the trick of using a wrapper functions
function _jumpFlyNow() { flyNow(); }
function flyNow() { yield is used in here }
in the example you can call _jumpFlyNow in situations where you are not allowed to call flyNow().
it's possible this is what you are getting at ?
Answer by grimmdev · Sep 04, 2012 at 05:23 AM
as @Fattie says and unity, you are technically looping your function, even if there is a condition, if it loops more then once, then it's a looping function. But fear not you do not need a yield, you could use a simple unity function InvokeRepeating, very handy. Then inside of the function your repeating thats invoked have your condition and if the condition is met or isnt depending how you want it you can cancel the invoke by CancelInvoke.
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.InvokeRepeating.html
function Start () { InvokeRepeating("example", 1.0, 1.0); }
function example () { if(condition1 == conditionvalue) CancelInvoke("example"); }
Your answer
![](https://koobas.hobune.stream/wayback/20220613080608im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Can't find Yield, LoadImageIntoTexture doesn't work with Texture 1 Answer
Setting Scroll View Width GUILayout 1 Answer
How to implement a delay between each time my gun fires (using coroutines or otherwise)?... 2 Answers