- Home /
Toggle Enabled/Disabled script?
I'm trying to enable and disable a script with the OnGui: Target variable. So far ive gotten:
> var toggleBool = true; function OnGUI() { var toggleBoolNew = GUI.Toggl (Rect (25, 25, 100, 30), toggleBool,"Toggle"); // Check if the toggle was toggled if (toggleBoolNew != toggleBool) { if (toggleBoolNew== true) Debug.Log("Toggle was enabled"); else > Debug.Log("Toggle was disabled"); > toggleBool = toggleBoolNew; } }
how do i modify this to make if enable/disable a script when the toggle is on/off?
Could you edit your post and fix the code formatting?
Answer by AliAzin · Jan 23, 2011 at 06:06 PM
You can use
go.GetComponent("your script name").enabled = false;// for disabling
go.GetComponent("your script name").enabled = true;// for enabling
you can use this on any Component.
you can put this in any script other than "your script name" script. note that "go" variable is the gameObject that the "your script name" belong to that.
So how would i put this in? Like where in the script that i put above would it go?
Answer by Igorotak · Apr 07, 2018 at 05:30 AM
bool toggleOn = false;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
toggleOn = !toggleOn
}
if (toggleOn)
{
//Do something when toggle is on
}
if (!toggleOn)
{
//Do something when toggle is off
}
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Setting Scroll View Width GUILayout 1 Answer
How can I set up a series of OnGUI Toggle buttons in a for loop? 1 Answer
GUI Toggle Using Button Style Remain Active 1 Answer
How to make gameObject and GUI button invisible/visible? 3 Answers