- Home /
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
Answer by BiG · Jan 19, 2012 at 07:14 AM
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;
They both have to be attached for this to work? i think thats what i did wrong :P
@Chompski, the code above works if both the scripts are attached to the same object.
However, if A and B are attached to different objects, you have to do this:
GameObject.Find("other_object_name").GetComponent(B).enabled = false;
hello ,I tried your method ,but error: 'eanbled' is not a member of 'UnityEngine.Component'
how can i fix it?thanks....
THAN$$anonymous$$ YOU Big Jan I have been looking for hours now and now it works!!!!!!!!!!! THAN$$anonymous$$ YOU, I can finally leave the Insane Asylum :P
Answer by Mander · Aug 14, 2012 at 02:19 PM
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;
Answer by kami1339 · Mar 28, 2019 at 12:28 PM
solved 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!!!!!
In the future, you should use the Code Sample button to format your code otherwise it's very hard to read:
public GameObject otherobj; //your other object
public string scr;
// your second script name
void Start () {
(otherobj.GetComponent(scr) as $$anonymous$$onoBehaviour).enabled = false;
}
Only workd if put in void start... i need it else where!
Answer by mehedihasanp78 · Jun 24, 2020 at 02:44 PM
Assuming both script attach to same game object
GetComponent<script name>().enabled = false;
this is amazing, best answer that worked for me
Answer by youssefcraft · Aug 27, 2021 at 04:33 PM
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