- Home /
Enable and Disable a script
What I'm trying to do is just enable and disable a script with a keypress. I know it's probably very simple, but I'm not very familiar with references to other scripts within a script. What I would like to have is something like this:
function Update () {
if (Input.GetKeyDown("l")) {
if (renderer.enabled == true)
renderer.enabled = false;
else
renderer.enabled = true;
}
}
but of course instead of enabling and disabling a renderer, enabling and disabling a script.
Answer by Eric5h5 · Feb 25, 2013 at 08:47 PM
Use GetComponent; you could do the same thing with the renderer since "renderer" is a shortcut for "GetComponent(Renderer)". By the way you could simplify that code (and replace the string with an enum) as:
function Update () {
if (Input.GetKeyDown(KeyCode.I)) {
renderer.enabled = !renderer.enabled;
}
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Show GUI texture then not 0 Answers
Turn on/off Script 3 Answers
Turn off multiple lights trigger 1 Answer