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.

have you tried using quotes aroud the script name

2 Answers

2

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.

The documentation is quite self explanatory

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')."