- Home /
Script.Enabled does not work in Unity 5+
Hello! I've read all topics about it but somehow I cannot make it work.
How can I disable a script using javascript in Unity 5+?
ScriptName.enabled = false;
GetComponent("ScriptName").enabled = false;
GetComponent(ScriptName).enabled = false;
None of the above codes are working for me. Thanks if you can help.
You should not use quotes, and doing so has been deprecated in Unity now.
Answer by Eric5h5 · Dec 27, 2016 at 09:01 AM
GetComponent(ScriptName).enabled works fine, no different than other versions. Nothing changed regarding that. You have some other issue.
It's possible, but I can't imagine what is happening. I tried the example Hellium said and all I get it:
"The name 'ScriptName' does not denote a valid type ('not found')."
Both scripts are attached at the same game object, no idea what should I try.
You wouldn't literally type "ScriptName" of course. But rather the name of the script.
Well yes, I was just explaining the situation. In the example bellow the name is "$$anonymous$$yscript.js". Anyway, thanks for trying.
Answer by Hellium · Dec 27, 2016 at 09:09 AM
The documentation is quite self explanatory
https://docs.unity3d.com/ScriptReference/Behaviour-enabled.html
You surely do something wrong in your script. Make sure you are referencing the right script.
With these very simple scripts, everything works
// MyScript.js
var otherScript : Rotate;
function Update () {
if( Input.GetKeyDown( KeyCode.Space ))
otherScript.enabled = !otherScript.enabled ;
if( Input.GetKeyDown( KeyCode.A ))
GetComponent.<Rotate>().enabled = !GetComponent.<Rotate>().enabled ;
}
// Rotate.js
#pragma strict
function Update () {
transform.Rotate(Vector3.up * Time.deltaTime * 50, Space.World);
}
Not sure what is happening here but I tried your example and got at least 5 errors:
"The name 'Rotate' does not denote a valid type ('not found')."
1st script is called $$anonymous$$yScript.js
while the 2nd one is called Rotate.js
I got it, I was speaking about not knowing what is going on with my Unity. Thank you very much for trying!