- Home /
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);
     }
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.
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
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                