- Home /
Disalbe/Activate script if a key is pressed.
How can I Disalbe/Activate some scripts if i press a specific key? so?
function Update() {
if (Input.GetKeyDown(KeyCode.X)) {
ScriptName.enabled = !Input.GetKey(yourKey);
ScriptName.disable = !Input.GetKey(yourKey);
}
Answer by fifthknotch · Mar 06, 2014 at 05:36 PM
You cannot attach this code to the script you want to disable because once disabled, it will no longer check for input. The way to get around this is to make a second script that will disable the first script. This second script will need to be something like (untested code):
var scriptToControl : nameOfScriptToControl;
function Start () {
scriptToControl = GetComponent("nameOfScriptToControl");
}
function Update() {
if (Input.GetKeyDown(KeyCode.X)) {
Debug.Log("User pressed X");
if (scriptToControl.enabled)
scriptToControl.enabled = false;
else
scriptToControl.enabled = true;
}
}
I was trying your script, but give me this error: The name 'nameOfScriptToControl' does not denote a valid type ('not found').
You need to put YOUR script name there. I just made up names to fill in the blanks
For example, i put "Serranda" as script, and in the inspector i drag the serranda.gameobect, but in play mode, in the inspector, the serranda.gameobject disappear. why?
In monodevelop you need to type your script names in the place of what I typed