- Home /
Yield only working once?
Hi, I have a question about the yield statement in the following code. The code only delays the first time the Attack () function is called. After that, there is no delay.
Is there something about yield statements that I don't understand? I thought they were pretty straightforward.
Here is the code I am using:
function Attack (attSpeed : float)
{
animation["attack"].layer = 2;
animation["attack"].AddMixingTransform(shoulder);
animation["attack"].speed = attSpeed * 2 * attackSpeed;
yield WaitForSeconds (3);
animation.Play("attack");
}
It depends on how you are using the 'Attack (float)' function. Can you show us the line where it gets called from?
Edit: How do I format the code in a comment?? Ctrl + k doesn't seem to work.
Sure, here it is: Both of these are from a different script.
function Attack ()
{
//print("attack");
if(!animation.IsPlaying ("attack"))
{
pPropertiesScript.Attack (Random.Range(attackSpeedRandom$$anonymous$$in,attackSpeedRandom$$anonymous$$ax));
}
if (squareDistToTarg > backupRange * backupRange)
{
dark$$anonymous$$nightAction = Dark$$anonymous$$nightAction.idle;
}
else if (trueDistToTarg > 0 && squareDistToTarg < backupRange * backupRange)
{
BackRight();
}
else if (trueDistToTarg < 0 && squareDistToTarg < backupRange * backupRange)
{
BackLeft();
}
}
This code is in a different script, and this Attack () is getting called in the Update () every frame that the target is in attack range. Here is the code that calls the Attack ():
if (squareDistToTarg < attackRange * attackRange)
{
Attack ();
}
Your answer
Follow this Question
Related Questions
Using yield wait for seconds in my script, correctly 2 Answers
Loop Animation with delay-variable 1 Answer
Make delay for spawn 3 Answers
How to Delay A Ball shooting script 1 Answer
How to delay a function (how to use WaitForSeconds() and yield) 1 Answer