Correct way to start and stop a Coroutine



CoroutineDemo.cs
using System.Collections;
using UnityEngine;

public class CoroutineDemo : MonoBehaviour
{
    IEnumerator coroutine;

    void Start()
    {
        Debug.Log(">>> Coroutine Started. <<<");
        coroutine = PintEvery1Sec();
        StartCoroutine(coroutine);
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (coroutine != null)
            {
                StopCoroutine(coroutine);
                coroutine = null;
                Debug.Log(">>> Coroutine Stopped. <<<");
            }
        }
    }

    IEnumerator PintEvery1Sec()
    {
        int i = 0;
        while (true)
        {
            i++;
            Debug.Log(i);
            yield return new WaitForSeconds(1);
        }
    }
}