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 Xephon99 · Oct 11, 2020 at 04:06 AM · coroutinewaitforsecondsdelay

Can IEnumerator be called more than once?

I have an Enemy script setup that chases a player with a chase range of 4. I then coded it to make the enemy be more aggressive if attacked, this would double the chase range. Next I coded for the enemy to wait 15 seconds and then reset to chase range of 4. My issues is that the reset only works the first time. If I go attack them again then run they IEnumerator does not run a second time to reset the chase distance/remove the aggressive tag. This is my first time using IEnumerator code, can anyone help me out?

 void Start()
 {
     startPosition = transform.position;
     StartCoroutine(CoUpdate());
     dchaseRange = 4;
 }

   void Update()
     {
         float playerDist = Vector2.Distance(transform.position, player.transform.position);
 
         if(playerDist <= attackRange)
         {
             // attack the player
             if(Time.time - lastAttackTime >= attackRate)
                 Attack();
 
 
             rig.velocity = Vector2.zero;
         }
 
         else if(playerDist <= chaseRange)
         {
             // Chase player
             Chase();
         }
         
         else
         {
             rig.velocity = Vector2.zero;
             transform.position = Vector3.MoveTowards(transform.position, startPosition, moveSpeed * Time.deltaTime);
             if(aggressive == true)
             {    
                 CoUpdate();
             }    
         }
     }
 
     IEnumerator CoUpdate()
     {
         if(chaseRange > dchaseRange)
         {
             yield return new WaitForSeconds(15f);
             chaseRange = dchaseRange;
             aggressive = false;
         }
     }
 
     void Chase ()
     {
         Vector2 dir = (player.transform.position - transform.position).normalized;
         
         rig.velocity = dir * moveSpeed;
     }
 
     
     public void TakeDamage (int damageTaken)
     {
         curHp -= damageTaken;
         aggressive = true;
         Agro();
         if(curHp <= 0)
             Die();
     }
     
     void Agro()
     {
         if(chaseRange == dchaseRange)
             chaseRange *= 2;
     } 
 
     void Die ()
     {
         // Give player xp
         player.AddXp(xpToGive);
         Destroy(gameObject);
     }
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 xibanya · Oct 11, 2020 at 04:09 AM

have a private Coroutine myRoutine or whatever, and if you want to call the routine again do something like

 if (myRoutine != null) StopCoroutine(myRoutine);
 myRoutine = StartCoroutine(WhateverRoutine());

the myRoutine variable will let you check on the status of the coroutine and stop it whenever you want.

Comment
Add comment · Show 1 · 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 Xephon99 · Oct 11, 2020 at 04:40 AM 0
Share

That is not really what I was wanting to do but Thank you as it got me on the right train of thought to fix the issue. I just added StartCoroutine(CoUpdate()); to the void agro section so it would ensure that it was running.

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

147 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

Related Questions

How to create a delay in a function being called in update? 2 Answers

Coroutine not working as expected 1 Answer

Coroutine not running after yield return new WaitForSeconds 3 Answers

How can i get this Delay script to work? 1 Answer

C# simple delay execution without coroutine? 2 Answers


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