Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

StopCoroutine not working

Discussion in 'Scripting' started by JPCannon, Dec 10, 2014.

  1. JPCannon

    JPCannon

    Joined:
    Sep 25, 2013
    Posts:
    37
    Hi. In my script if I hit monster which have this script attached i set him imOnIce bool to true. This working fine. When imOnIce is true i change monster speed to 0 and after this start coroutine which change speed again after 5 seconds. Problem is when i try hit monster again after first and whene old freeze still working. I won`t to stop old coroutine and start all think from begin. So when i hit monster i check that monster have imOnIce true and if it is i stopCoroutine. Unfortunately even my script is enter in stopCoroutine it dosent work and my last coroutine end after 1/2 seconds. What`s wrong?

    Code (CSharp):
    1.    
    2. void OnTriggerEnter2D (Collider2D collider) {
    3.  
    4.         ShotScript shot = collider.gameObject.GetComponent<ShotScript> ();
    5.        
    6.         if (shot != null)
    7.         {
    8.         if (shot.isHit != isEnemy)
    9.             {
    10.                 PlayerData.hittedEnemys++;
    11.                 EffectOn(PlayerActiveSkills.actualActiveSkill);
    12.                 HP -= shot.damage;
    13.                 enemyHpBar.value = HP;
    14.  
    15.                 if(imOnIce) {
    16.                     speed = new Vector2(0,0);
    17.                     StartCoroutine("ChangeSpeedAfterTime", 5);
    18.                 }
    19.  
    20.             }
    21.         }
    22.    
    23.     }
    24.  
    25.     void EffectOn( SkillType effectType ) {
    26.  
    27.         if(effectType == SkillType.IceArrow) {
    28.             if(imOnIce) {
    29.                 Debug.Log("IM IN");
    30.                 StopCoroutine("ChangeSpeedAfterTime");
    31.             }
    32.             imOnIce = true;
    33.         }
    34.     }
    35.  
    36.  
     
  2. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    I believe this is our problem
    "Please note that only StartCoroutine using a string method name can be stopped using StopCoroutine."
    -docs

    Instead of
    Code (CSharp):
    1. StartCoroutine("ChangeSpeedAfterTime", 5);
    use
    Code (CSharp):
    1. StartCoroutine(ChangeSpeedAfterTime(5));
     
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Haven't you got that the wrong way around?
    He IS currently using string names, you are suggesting he startCoro directly using the IEnumerator method
     
  4. JPCannon

    JPCannon

    Joined:
    Sep 25, 2013
    Posts:
    37
    So if i use coroutine with string what can be wrong? :p I`m using

    Code (CSharp):
    1. StartCoroutine("ChangeSpeedAfterTime", 5);
    So this is good form i think
     
  5. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Did you try it without a parameter?
     
  6. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    *self directed facepalm*
    you are correct my mistake
     
  7. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    Ok I found a stackoverflow entry here that may explain your problem although I haven't verified this information.

    it says
    I take this to mean your StopCoroutine isn't working because it can't be stopped during the 5 seconds it's waiting because it's not executing. It would stop the next time you called yield [something] but since I'm assuming you only have one yield statement this never happens.

    Assuming this is actually the issue, you could fix it by modifying your coroutine. Instead of doing
    Code (CSharp):
    1. yield return new WaitForSeconds(waitTime);
    you could do something like
    Code (CSharp):
    1. for (float timer = waitTime; timer >= 0; timer -= Time.deltaTime){
    2.             yield return 0;//this basically just yields until next frame
    3. }
    4. //do whatever happens after the time runs out here