Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by WeirderChimp · Dec 13, 2014 at 01:57 PM · waitforsecondsienumerator

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
 }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image WeirderChimp · Dec 13, 2014 at 02:54 PM 0
Share

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

avatar image SkaredCreations · Dec 14, 2014 at 12:42 PM 0
Share

Everything can be virtual and overridden, also IEnumerator (I use it sometimes when needed).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How can I instantiate and move objects smoothly? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges