Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Neferlem · May 01, 2019 at 12:29 PM · scripting problemcoroutinestopcoroutine

Reset a coroutine on event

Hello everyone,

I'm struggling with a coroutine. I'm using it to make a random timer when enemy is attacking my player. If nothing else is happening, it's running just as I want. Here is the code :

 randomWait = Random.Range(2, 4); //It generates a random number
 yield return new WaitForSeconds(randomWait); //Enemy waits the returned number
 animator.SetTrigger("Push"); //Then it attacks.

But, I'm trying to reset it when enemy is taking a hit. I tried with if/else, with a yield return null but enemy behaviour is quite random. Sometimes I'm attacking him, enemy falls, gets up and is attacking while I'm not at range of its attacks. It looks like I'm attacking him just before "Push" is triggered, and when its "hurt" animation is over, it resumes coroutine.

How to completely stop that coroutine, cancel any instruction and make it ready for next start ? I alos tried with stopcoroutine(mycoroutine) but no results so far...

Thanks

Comment
Add comment · Show 3
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 akaBase · May 01, 2019 at 12:52 PM 0
Share

You could use something like this if(randomWait < Time.time || enemyIsHurt){return null;} where the enemyIsHurt is a bool you use to know when it is available to attack, also in your Random.Range(2, 4) change them to floats, at the moment you are returning ints so your range is limited to second increments

avatar image Neferlem akaBase · May 01, 2019 at 06:44 PM 0
Share

I tried with your code, but now enemy can't attack. What is the meaning of randomWait < Time.time ?

avatar image akaBase Neferlem · May 02, 2019 at 11:07 AM 0
Share

waitUntill/randomWait = Time.time + Random.Range(2, 4); Like that so it sets a ranom time either 2 or 3 seconds from when you set it, Hense why I said about using floats so that it can be between the range not just the second increments.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Neferlem · May 06, 2019 at 08:30 PM

I tried with another method : I created a function that launches at the end of enemy animation. My goal was to avoid enemy making an attack when It finishes its invulnerability frames. So I made a marker at the end of animation, that launches a StopCoroutine(myCoroutine); At first StopCoroutine was cancelling coroutine without giving it possibility to restart, now it properly restart. Using some debug.log, I could check when coroutine launched, stopped and restarted. Thanks @fafase and @Bciam for your kind help

Comment
Add comment · 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

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

184 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 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 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 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 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 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 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 avatar image avatar image

Related Questions

NullReference exception after StopCoroutine 2 Answers

I'm stopping coroutines in pause but can't bring them back 0 Answers

[HELP] How to make camera smooth change position while follow object? 1 Answer

StopCoroutine not returning to yield 0 Answers

Can you run a coroutine more often than Update? 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