- Home /
WaitForSeconds in virtual void. having trouble understanding IEnumerator
hey so im making a 2d game and for my enemys attacking they have a base class called MeleeAttack. my first enemy is a slime it has a script called Slime_MeleeAttack which inherits MeleeAttack. this is so i can have custom attacks for different enemy's but not have to rewrite the script multiple times. im trying to have a delay so the enemy starts its attack but the damage isnt applied for X amount of seconds. here is my code:
public virtual void Attack (Transform target)//this is called by the the entity. IT Can be overriden by an attackmelee script
{
if(CanAttack)
{
//Need to PLAY ANIMATIONS HERE
Debug.Log (Time.time + " first");
StartCoroutine(WaitToDamage(DamageDelay));
Debug.Log (Time.time + " Second");
target.GetComponent<Entity>().TakeDamage(Damage);
}
}
IEnumerator WaitToDamage (float delay)
{
Debug.Log(delay);
yield return new WaitForSeconds(delay);
yield return null;
}
the Main Problem im having is that when i call StartCoroutine(WaitToDamage(DamageDelay)); it doesnt wait for WaitToDamage to finish before calling Debug.Log (Time.time + " Second");. i need the Attack function to wait till WaitToDamage is finished then continue running the code.
can this be done if so please explain because i dont understand this. OR is there a better way of doing it
Thanks ~Scott
PS is this post of any relivence http://answers.unity3d.com/questions/276055/question-about-coroutines-and-waiting-for-a-functi.html
Answer by SkaredCreations · Dec 13, 2014 at 02:01 PM
To wait for a coroutine to finish the function that calls the coroutine must be a coroutine itself so that you can use this code:
yield return StartCoroutine(WaitToDamage(DamageDelay));
Else you can just move the whole logic into WaitToDamage:
public virtual void Attack (Transform target)//this is called by the the entity. IT Can be overriden by an attackmelee script
{
if(CanAttack)
{
StartCoroutine(WaitToDamage(target));
}
}
IEnumerator WaitToDamage (Transform target)
{
//Need to PLAY ANIMATIONS HERE
yield return new WaitForSeconds(DamageDelay);
target.GetComponent<Entity>().TakeDamage(Damage);
}
I cant make Attack() a corutine becasue i dont think i can then use the virtual so i can be overridden by my slime_meleeAttack. and id rather not have target.GetComponent().TakeDamage(Damage); in my WaitToDamage becasue i want to be able to override the damaging stuff in another script. the best solution i have is have another function canned FinishAttack which is virtual so i can be overridden and call it after my wait for seconds. and so far its working its just a bit messy and probs not a very good way of doing it. anyways thanks for replying. and soz if people cant understand what im trying to say im tired and it 2am in the morning
Everything can be virtual and overridden, also IEnumerator (I use it sometimes when needed).
Your answer
Follow this Question
Related Questions
WaitForSeconds does not work 0 Answers
yield return waitforseconds not waiting? 3 Answers
Pause script until animation is finished. 1 Answer
IEnumerator issues 3 Answers