StopCoroutine not working

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 wont 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. Whats wrong?

void OnTriggerEnter2D (Collider2D collider) {

        ShotScript shot = collider.gameObject.GetComponent<ShotScript> ();
       
        if (shot != null)
        {
        if (shot.isHit != isEnemy)
            {
                PlayerData.hittedEnemys++;
                EffectOn(PlayerActiveSkills.actualActiveSkill);
                HP -= shot.damage;
                enemyHpBar.value = HP;

                if(imOnIce) {
                    speed = new Vector2(0,0);
                    StartCoroutine("ChangeSpeedAfterTime", 5);
                }

            }
        }
   
    }

    void EffectOn( SkillType effectType ) {

        if(effectType == SkillType.IceArrow) {
            if(imOnIce) {
                Debug.Log("IM IN");
                StopCoroutine("ChangeSpeedAfterTime");
            }
            imOnIce = true;
        }
    }

I believe this is our problem
“Please note that only StartCoroutine using a string method name can be stopped using StopCoroutine.”
-docs

Instead of

StartCoroutine("ChangeSpeedAfterTime", 5);

use

StartCoroutine(ChangeSpeedAfterTime(5));

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

So if i use coroutine with string what can be wrong? :stuck_out_tongue: I`m using

StartCoroutine("ChangeSpeedAfterTime", 5);

So this is good form i think

Did you try it without a parameter?

self directed facepalm
you are correct my mistake

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

yield return new WaitForSeconds(waitTime);

you could do something like

for (float timer = waitTime; timer >= 0; timer -= Time.deltaTime){
            yield return 0;//this basically just yields until next frame
}
//do whatever happens after the time runs out here