- Home /
What does 'enabled' do in isolation within a script?
Hi
In the MachineGun script from the Unity FPS tutorial is this line:
enabled = false;
I looked in the script reference and found this:
"Behaviour.enabled Enabled Behaviours are Updated, disabled Behaviours are not."
Does this mean that the Update(), FixedUpdate() and LateUpdate() functions are not called within only the script in which that line appears? Or, as Behavior is a super-class, is it affecting other scripts attached to the parent object?
Thanks
Answer by Statement · Jan 14, 2011 at 12:13 PM
It affect only the instance. If a game object have several behaviors, disabling one will not affect the others. If you have a script and write enabled = false; it will only disable the script that is running. Other scripts will remain enabled. To disable another script, you should set enable on that particular instance instead. renderer.enabled = false; for example turns off the renderer.
There is also gameObject.active which disable the game object and any components attached to it.
Thanks Statement. If a script can disable itself this way, how do you re-enable it then? Surely as the script is no longer running, having "enabled=true" will never be called?
Sort of true. Some functions are still called even if it's disabled. Check the manual which functions this applies to. IIRC collision events are still sent. You can use another object to enable something. IIRC Invoke functions run even though object is disabled.
IIRC InvokeRepeating can be used to check for some condition is met, to re-enable the object.
Thanks, InvokeRepeating is really useful. Looking through the manual it seems most functions except Late/Fixed/Update are still called.
Your answer
Follow this Question
Related Questions
gameObject.GetComponent("Script").enabled = true not working 5 Answers
Enable script on parent 2 Answers
Information about Behaviour.enabled 1 Answer
Errors when trying to enable script on gameobject. 0 Answers