- Home /
GUI.Toggle to only send one action, not repeated
Hi people,
I've set up a script with a number of toggle buttons. I have them switching on and off when clicked which is fine. When clicked, a function is performed, but it keeps sending the message. What I want to do is just send a single message/action once. This is obviously a standard button action, but I couldn't figure out how a button could have an on/off state; change graphic when touched.
Here's my code:
#pragma strict
var LeftNavSkin: GUISkin;
//var toggle = true;
var bt1 = false;
var bt2 = false;
var bt3 = false;
// Clears everybody and returns true to help setting the desired one
function setMeOnly():boolean{
bt1 = bt2 = bt3 = false;
return true;
}
function OnGUI () {
GUI.skin = LeftNavSkin;
GUI.Box (Rect (-30,10,100,90), "");
//Top Left Button
if (GUI.Toggle (Rect(50,300,100,100), bt1, "")) bt1 = setMeOnly();
//Middle Button
if (GUI.Toggle (Rect(50,410,100,100), bt2, "")) bt2 = setMeOnly();
//Bottom Right Button
if (GUI.Toggle (Rect(50,520,100,100), bt3, "")) bt3 = setMeOnly();
//This sends the debug.log repeatedly. I want it to just send once.
if(bt1 === true)
{
Debug.Log("OMG IT WORKS");
}
}
Help would be appreciated!
Rich
Answer by Jamora · Jul 19, 2013 at 12:38 PM
If you only ever have one button on at one time, you can use a SelectionGrid with a custom style. Define your Style in a GUISkin. You can set the different textures for when the button is pressed/hovered over etc. via the inspector.
As for your function problem, after the SelectionGrid, use if(GUI.changed) MyFunction(); Alternatively keep two ints, one for the old value, one for the new and then check each frame if the old value matches the new one.
Thanks. That is the kinda thing I'm looking for. Can you tell me how you arrange the grid differently. It's 4 up at the moment. I would like to go 4 in a line down for example.
Thanks again