How do i disable a script from a different script?

Im just having issues setting it up as a var unless im doing it completely wrong which is likely

Suppose that you are executing script A, and you want to disable script B. Both of them are attached to the same object. That’s an easy task:

GetComponent(B).enabled = false;

what you need to disable a script or a component from anyscript, is to find the gameObject where ur script is attached and then enable = false/true;

u can find ur game object by name or tag. store it in one variable and disable

for example C#:

GameObject varGameObject = GameObject.Find("object");   or find with tag 

GameObject varGameObject = GameObject.FindWithTag("Player"); then disable or enable script/component

varGameObject.GetComponent<scriptname>().enabled = true;

example Js:

var varGameObject = gameObject.Find("object")   Or with tag

var varGameObject = gameObject.FindWithTag("player");

varGameObject.GetComponent(scriptname).enabled = true;

Assuming both script attach to same game object

GetComponent<script name>().enabled = false;

use this sample

Use this code

public GameObject otherobj;//your other object
public string scr;// your secound script name
void Start () {
(otherobj. GetComponent(scr) as MonoBehaviour).enabled = false;

it will work!!!

Thanks a TON!

public ScriptName variableName;

ScriptName is the name of the script you want to access
Variable name is just the name of the variable that contains the script
you gotta assign it btw

After searching on youtube, I found a very easy solution as of 2022:

Say you can Script A assigned to Cube A, and Script B assigned to Cube B and you want to disable Script A by using Script B.

Outside of any methods, create a Behaviour instance field in Script B, naming it something related to disabling Script A. Then assign Cube A to your behavior field in unity.201909-coding-ss.png

(my method is called stopPlayer)

Then simply using (your command).enable = false; (or true to reenact your code) will disable and enable your code! My code looks like this (minus the extra mumbo jumbo):

public class SettingsScripts : MonoBehaviour
    public Behaviour stopPlayer;

    void Update()
        if (Input.GetButton("Cancel"))
                stopPlayer.enabled = false;

    public void closeSettings()
        canvasObject.GetComponent<Canvas> ().enabled = false;
        stopPlayer.enabled = true;

I hope this helps anyone else still looking for answers!