- Home /
disable script on trigger
Hi, I'm trying to make it so after I press the e button on an object it disables the script that was used but it's not working when I try it
if(Input.GetKeyDown("e"))
{
(GetComponent("lever2.cs") as MonoBehaviour).enabled = false;
}
}
or at least tell me a way to disable all keys too.
Comment
We need a bit more info than that. Is a component of type lever2 attached to the same GameObject? If not, you need to specify which GameObject lever2 is attached to and get the component from that object. Alternatively, you could use Unity's built-in (but rather slow) method to find all $$anonymous$$onoBehaviours/Objects of a specific type. Also, the most common way of calling GetComponent is:
GetComponent<type>().enabled = false;
Additionally, classes are CamelCased in Unity. This should help making your code more readable.