- Home /
Toggle Scripts On & Off
Lets say i press Q and it does
GetComponent(Script).enabled = false;
Ofcourse that works great one way but how can i set it so when i press Q again it will do
GetComponent(Script).enabled = true;
Like a switch between them. Javascript btw...
Thanks :) Also im only learning its most likely a very simple solution but no idea how :(
Answer by Justin Warner · Apr 23, 2011 at 11:22 PM
http://unity3d.com/support/documentation/ScriptReference/Input.html
Read up on that.
But:
function Update () {
if (Input.GetButtonDown ("q")) {
if(GetCompnent(Script).enabled=true)
{
GetCompnent(Script).enabled = false;
}
else
{
GetCompnent(Script).enabled = true;
}
}
}
Right, just make sure the script you reference is not the script youre executing this from. Because then when you turn it of, it can no longer register if you turn it on :p
So I can put this in an empty game object then have an on screen button to switch between the characters(scripts)?
indeed, with some adjustments, you could put this on empty game object, reference your character objects/scripts. And have a button on screen to switch between them.
But the basic logic is here.
but in c# it says that GetComponent("Script name" ) doesn't have method ".enabled" why so??
Your answer
![](https://koobas.hobune.stream/wayback/20220613124801im_/https://answers.unity.com/themes/thub/images/avi.jpg)