Unity learning — stop coroutine

The StopCoroutine method is similar to the StartCoroutine method, with two overloads.
void StopCoroutine(string methodName)
void StopCoroutine(IEnumerator routine)
this method can either pass in the methodName of the coroutine method as a parameter of type string or of type IEnumerator. Next, the StopCoroutine method is used:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StopCoroutine : MonoBehaviour {

    IEnumerator DoSomething(float someParameter)
    {
        print("DoSomething Loop");
        yield return null;
    }
     IEnumerator Start()
    {
        StartCoroutine("DoSomething",2.0f);
        yield return new  WaitForSeconds(1);
        StopCoroutine("DoSomething");
    }
}

This code opens a coroutine called DoSomething, which, if run continuously, prints out the phrase “DoSomething Loop”. So after waiting a second, the code stops the coroutine when it executes on the StopCoroutine line.

note: this is when the StopCoroutine method is not used

note: this is when the method is used
The StopCoroutine method can only stop the same coroutine in the same game script with the same Chinese name and string parameter passed in, and cannot affect the coroutine opened in other scripts. The StopCoroutine method, meanwhile, can only StopCoroutine that was started with an overloaded version of StartCoroutine’s string argument.

Read More: