- Home /
What exactly does script.enabled = false turn off
As the title suggests, if i use getcomponent(script).enabled = false, what exactly turns off.
Lets say i have Update() FixedUpdate() OnTriggerEnter() OnTriggerStay()
So upon disabling the script I've read that Update() gets turned off, but do OnTrigger Enter and OnTriggerStay still function?
The reason I'm asking is, I have intensive code running in update on a character. If that character dies I want that code to stop running BUT the dead character with a collider still exists so i have some Set() functions accessed from a second script on collision that affect some private dead characters variables.
If i disable the script upon death, would the Set() functions still function if i access them from a second script. The Set() functions are completely outside Update() or any loop for that matter.
Thanks.
Answer by robertbu · Mar 06, 2014 at 10:40 PM
According to the reference for 'Behaviour', the 'enable' flag impacts updating, so if I disable the flag, I would expect Update() and FixedUpdate() to stop being called, but all other functions to continue to be used. When I have questions like this, I usually just take a few minutes and test things out. Here is a script that tests the callbacks you ask about (and a couple of others).
function Start() {
InvokeRepeating("Repeating", 0,1);
ACoroutine();
enabled = false;
}
function Repeating() {
Debug.Log("Repeating");
}
function ACoroutine() {
while (true) {
Debug.Log("Coroutine");
yield WaitForSeconds(1.0);
}
}
function Update () {
Debug.Log("Update");
}
function FixedUpdate() {
Debug.Log("FixedUpdate");
}
function OnGUI() {
Debug.Log("GUI");
GUI.Label(Rect(0,0,100,25), "GUI Label");
}
function OnMouseDown() {
Debug.Log("Mouse down");
}
function OnCollisionEnter() {
Debug.Log("Collision");
}
function OnTriggerEnter() {
Debug.Log("Trigger");
}
function PublicFunctionToCall() {
Debug.Log("Public Function");
}
Running it, I found the following still worked when the script was disabled:
Coroutines
InvokeRepeating() calls
OnCollisionEnter
OnTriggerEnter
OnMouseDown
Calls by other scripts
What stopped running:
Update
FixedUpdate
OnGUI
??!! That is against anything you would expect to be logical!! Not that the answer is wrong but disabling a script I would expect it to do nothing, but it seems most of the event callbacks are in use... good to know but I consider this a wrong implementation from Unity.
I believe that on top of this, Start won't be called on a script that is not enabled, but Awake will.
It's intentional behavior, so you can make scripts "sleep" by turning off Update, but have the option to wake themselves up in OnTriggerEnter or whatever. I think the the property name is misleading though...ins$$anonymous$$d of ".enabled", something like ".updatesEnabled" would probably be better.
Answer by sirdudalot · Oct 17, 2016 at 07:30 PM
robertbu nailed it, but I wanted to add that I just put a little...
if (!enabled) return;
... at the beginning of the trigger and collision functions when I want them to also be disabled.
Thank you! This is such a simple solution to some of the problems I've been encountering.
Your answer
![](https://koobas.hobune.stream/wayback/20220613135542im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
onMouseDown call function from other script 1 Answer
Making a var appear on click 1 Answer
Function triggered when scene changes? 4 Answers
gameObject.tag doesn't work as I'd expect inside a function Update 0 Answers
How do i run scripts more times 2 Answers