- Home /
Coroutine a function within a loop?
hey I'm working on a project in which I have a function that plays a punch animation. after 0.4 seconds the the punch changes into a kick (sort of like a combo). to make this happen I used yield waitforseconds(0.4). for some reason the function doesn't seem to be working. but when I take the yield out the function works fine (exept that it skips the punch animation and goes directly to the kick.) Is it imposible to coroutine a function within a while loop?
here's my code, to give you an idea of what I am doing.
var attackf = function(theclass : String){
if(thenewscript.hit == false){
attack = true;
if(theclass == punch){
sanimation.Play("punch");
yield WaitForSeconds(0.4);
sanimation.Play("kick");
attack = false;
}
}
};
while(true){
if(Input.GetKeyDown("n")){
attackf("n");
}
yield;
}
this strangely has no response when I call it from within my while loop, (and yes it is not possible for me to make this an update function). n is equal to the punch variable.
that wasn't my whole while loop mind you.
but when i change my function to this...
var attackf = function(theclass : String){
if(thenewscript.hit == false){
attack = true;
if(theclass == punch){
sanimation.Play("punch");
sanimation.Play("kick");
attack = false;
}
}
};
The animation changes, but not the way i need it too.
Any suggestions? all help is greatly appreiciated. Sorry if this question has an obvious problem that I overlooked, like that time I spelt my function On Tigger Enter.
P.S. I'm using Unity 2d toolkit, does that change anything
$$anonymous$$h... probably not related to your problem, but..: Do you want your main loop to continue after spawning the animations? $$anonymous$$aybe you wanted to change the call from attackf()
into yield attackf()
to pause the loop until the attack finished? If not, you have to make sure that attackf
is not constantly called each frame as long as the user presses 'n' doing havoc with your sanimation states..?
Yes, it is necessary. I plan to use this function to hold multiple attacks that will be run by different input, (which must be in there own if statements for other reasons).
The function doesn't reek havoc because, as i stated above, that was not my whole while loop. I didn't include it all because it was 30 lines long.
Within it as well are functions for for movement. Originally I had the punch script in an update function in an outside script, but when the run button, and the punch button were pressed, there was some serious animation issues, in which two animations played at once.
I just want to know if my current script can work with the coroutines or do I have to do something else.
Yes that works perfectly! sorry about my lack of knowledge of monobehaviors, I'm relatively new at this.
Thanks a lot. I only wish you had answered so that i could select it as the correct answer.
If a ad$$anonymous$$ or someone else sees this could you please close this question, I can't do it myself
Thanks a lot everyone!
ok i converted my comment to an answer not to confuse people :-)
No worries we are all learning non stop
Answer by SolidSnake · Jun 10, 2013 at 11:02 AM
Don't you need to call the function as StartCoroutine(attackf("n"));
?