- Home /
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<clonefor4>().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!
Answer by IINovaII · Dec 22, 2019 at 07:25 AM
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
orStopAllCoroutines
functionsDisable the gameobject which the script is attached to instead of just disabling the script.
I was working on a door function with two scripts. One for the door, and another for a broken door. Silly me didn't know that scripts still work when disabled and that is a hard lesson learned. People, don't be the guy wondering why your scripts suddenly stop working, don't spend 7 hours like me :]
Your answer
Follow this Question
Related Questions
How to clone randomly 2 0 Answers
How to stop all StopAllCoroutines(); from another script 1 Answer
How to clone with a object. 3 Answers
Switch between two light sources 3 Answers
Cloning a game object. 1 Answer