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 question about Coroutines and waiting for a function to finish - Unity Answers