- Home /
Information about Behaviour.enabled
Hey there, I quick question.
If i disable script in Start() function like this.
void Start(){
GetComponent<MyScript>().enabled = false;
}
What exactly it disables. All update functions like Update(), OnGUI(), OnTriggerEnter/Exit, OnCollision and all others, or just specific ones, thanks.
Answer by Invertex · Jan 05, 2014 at 04:14 PM
It disables the active part of the script. Functions can still be called externally. http://unity3d.com/support/documentation/ScriptReference/Behaviour-enabled.html?from=MonoBehaviour
Something like this you could have tested very quickly! And there are a few answered questions about this on here already ;)
I know that variables and function can still be called, that is why i am asking which "active" part, all, or something is still left to update.
$$anonymous$$ost script are plain methods and variables and called only when needed, without any needs to update anything. Couldn't that be a good practice to disable scripts that aren't in need in any updating to save those empty calls?
You can just leave the update function out completely if you're not using it, it's not required. But if you want to stop a script from updating for a while, it's a nice choice ins$$anonymous$$d of using a condition in the update itself.
If you disable a script, the component itself doesn't fire any callbacks. However other components might fire some callbacks that belongs to them if your disabled script implements the callback. A common example is OnBecameVisible. It is fired from an attached renderer and will execute even when your script is disabled. Note: if the gameObject has been deactivated nothing will happen since the whole gameobject (including the renderer) is dead.
Update, FixedUpdate, OnGUI are not part of the $$anonymous$$onoBehaviour base class. If they were you would have to override all the callbacks. You actually implement the callback "from scratch". Unity checks the compiled code (probably with reflection) to figure out which classes implement which callbacks. Based on this metadata Unity invokes the callbacks. So if you don't implement Update, Start, ... it won't be called.
Try this: Create an empty class (remove Start and Update) and attach it to a gameobject. Unity, at least in the older versions, even hides the enable/disable checkbox in the inspector since enabling wouldn't do anything. As soon as you add Start or Update the checkbox will reappear.
Your answer
Follow this Question
Related Questions
Disabling THIS script from code 1 Answer
Enabling/Disabling Multiple Tagged objects 1 Answer
Disalbe/Activate script if a key is pressed. 1 Answer
disable a script when a object is hidden? 1 Answer
A do nothing script? 2 Answers