Disabled script still runs.

Yes, I know this is a duplicate but I am very confused. I disable a cloning script kindly attained by the nice people who answer here and if I switch it off it still runs, here it is.

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

public class clonefor4 : MonoBehaviour
{
public GameObject MyObjectToClone;
Vector3 offset = new Vector3(2, 0, 2);

// Start is called before the first frame update
void Start()
{
    StartCoroutine("Spawner");
}

IEnumerator Spawner()
{
    while (true)
    {
        GameObject clone = Instantiate(MyObjectToClone, transform.position + offset, Quaternion.identity);
        clone.transform.position = new Vector3(clone.transform.position.x, 8f, clone.transform.position.z);
        clone.transform.position = transform.position + offset;
        clone.transform.position = new Vector3(clone.transform.position.x, 4f, clone.transform.position.z);
        yield return new WaitForSeconds(10);
    }

}

} but if I disable it FindObjectOfType().enabled = false;It still functions, is there any way to completely shut off the script? I know if (!enabled) return;` has something to do with it. Any ideas? Thanks!

The reason why the coroutine still runs is because you are only deactivating the script. Coroutine will still run even if the script is disabled as long as the gameobject it is attached to is enabled. In order to completely stop the coroutine, you can try one of the following methods.

  • Stop the coroutine using StopCoroutine or StopAllCoroutines functions
  • Disable the gameobject which the script is attached to instead of just disabling the script.